I just create an image that comes from the camera on an Android application:
YuvImage yuv = new YuvImage(
byteArrayDataFromCamera,
camera.getParameters().getPreviewFormat(),
imageWidth,
imageHeight, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, imageWidth, imageHeight, 100, out);
byte[] bytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
And I want to add Exif information to it. Since now I've tried this:
// Save image on SD
storeImage(image, "Image_0.jpg");
String filePath = Environment.getExternalStorageDirectory() + "/MyFolder/Image_0.jpg";
try {
ExifInterface exif = new ExifInterface(filePath);
exif.setAttribute("UserComment", "my custom comment");
exif.saveAttributes();
}
catch (IOException e) {
e.printStackTrace();
}
The storeImage method:
private boolean storeImage(Bitmap imageData, String filename) {
//get path to external storage (SD card)
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/MyFolder/";
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
try {
String filePath = sdIconStorageDir.toString() + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
//choose another format if PNG doesn't suit you
imageData.compress(CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
}
return true;
}
As my image is recently created, it doesn't have any ExifInterface information. I want to know how to add new ExifInterface to my recently created image. How can achieve this?
I know that you can add rotation in image information but to add comment it's more complicated. The Exif class doesn't work correctly... Because you can read Exif information from stream but you can write this information. When I search information to add image info the users have the same problem as you, and I know that existing libraries to add information in image information...
When I find the post I give you the URL!!
I can find the post, read this!!
Tell me if I helped you and good programming!
Related
I save image from custom camera:
public String getFilename() {
File file = new File(Environment.getExternalStorageDirectory().getPath(), "Products/Images");
if (!file.exists()) {
file.mkdirs();
}
String uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
return uriSting;
}
Now:
public String saveImage(Bitmap bitmap) {
FileOutputStream out = null;
String filename = getFilename();
try {
out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (FileNotFoundException e) {
Log.e("Error",e.toString());
e.printStackTrace();
return "";
}
return filename;
}
I use this filename in exifInterface after saving image
exif = new ExifInterface(filePath);
But every time it shows in my log Raw image not detected, Exif: 0
You compress a bitmap to a jpg file.
Bitmaps dont contain exif information.
And compressing a bitmap to a jpg file does not add an exif header to the file.
BitmapFactory.decodeByteArray(data, 0, data.length);
You have a nice byte array called data.
This byte array contains the bytes of a jpg file with exif header.
If you wanna save the data to file then do not make an intermediate bitmap first as at that moment you lost the exif header.
Instead save the bytes in your data array directly to file.
Then you will have a jpg file with an exif header.
Ok i'm completely editing this post... I have made it so that I can save the file path to my data base. this works and is saved as /storage/emulated/0/1508blah blah.jpg . Now i cannot get my code to read this item back into a picture.
imagePhoto = (ImageView)findViewById(R.id.detail_recipe_image);
Toast.makeText(this, recipe.image, Toast.LENGTH_SHORT).show();
Bitmap bmp = BitmapFactory.decodeFile(String.valueOf(recipe.image));
imagePhoto.setImageBitmap(bmp);
am I missing something here? cause the Toast Is reading the recipe.image just fine and is displaying the path. why Is the rest not displaying the image?
Storage Code
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
String picturePath = destination.toString();
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
textImagePath.setText(picturePath.toString());
ImageView img = (ImageView)findViewById(R.id.addphotoview);
img.setImageBitmap(thumbnail);
}
Adding in the files paths seem to be the best solution to the problem i am having so that you #ModularSynth for your help with this. Always making sure all the info is your code to make the file paths work helps.
Hi I am working on application which gets .png files in byte stream from server. When I got it I try to make from it bmp and later convert it to .png file, but the following method ( Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length); ) returns me null.
Here is my code:
byte[] result
Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
try {
File filename = new File(imageUri.getPath()+name);
File parentFile = new File(imageUri.getPath());
parentFile.mkdirs();
FileOutputStream out = new FileOutputStream(filename);
img.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (FileNotFoundException e) {
Log.e("imageDownloaded", e.toString());
} catch (Exception e) {
Log.e("imageDownloaded", e.toString());
}
but the img Bitmap is always, null. I uploaded the image as multi part data and it was png file parsed to byte array, but now when i want to retrieve it i get this ugly null. Thanks for any help.
I want to add image to android PDF using iText. I want to achieve this without saving image to SDCard first. I put my image into res/drawable folder but proving the image path doesn’t work and it throws FileNotFound Exception. My path is like this:
String path = “res/drawable/myImage.png”
Image image = Image.getInstance(path);
document.add(image);
Now please suggest me a solution how I will add correct file path to getInstance(…) method. Thanks
Of course it'll not work at that way.
move your image to assets folder to access it with getassets() method
// load image
try {
// get input stream
InputStream ims = getAssets().open("myImage.png");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
document.add(image);
}
catch(IOException ex)
{
return;
}
I found a solution for your issue. If you want to get image from your drawable folder and put it into a PDF file using iText use this code:
try {
document.open();
Drawable d = getResources().getDrawable(R.drawable.myImage);
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bmp = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
document.add(image);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
Here is the code to add image to PDF using iText, if the image is dynamic (i.e), if the image cannot be added to asset folder at compile time,
public void addImage(Document document,ImageView ivPhoto) throws DocumentException {
try {
BitmapDrawable drawable = (BitmapDrawable) ivPhoto.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageInByte = stream.toByteArray();
Image image = Image.getInstance(imageInByte);
document.add(image);
}
catch(IOException ex)
{
return;
}
}
Here is my code, To set Image on particular position
move your image to assets folder to get image by getassets() method.
Hope this will help you!
try {
InputStream ims = getAssets().open("header1.png");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
image.setAbsolutePosition(10f,750f);
image.scaleToFit(850,78);
document.add(image);
}
catch(IOException ex)
{
ex.printStackTrace();
return;
}
try {
FileInputStream in = new FileInputStream("input file uri");
PdfReader pdfReader = new PdfReader(in);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("output file uri"));
PdfContentByte content = pdfStamper.getOverContent(1);
Image deliverImg = Image.getInstance("image URI");
deliverImg.setAbsolutePosition(420f, 100f);
content.addImage(deliverImg);
pdfStamper.close();
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
I am trying to open a bitmap that has already been stored in SdCard as follows:
String imageFilePath= "/sdcard/SoftCopy/"+mybitmap.png;
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
Bitmap loadedWork= BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
I have a second Bitmap named currentWork. This bitmap is actually the current drawing that has been done. I have combined two bitmaps as follows:
Canvas c = new Canvas(loadedWork);
c.drawBitmap(currentWork, 0, 0, null); //so that currentWork get drawn on loadedWork
Now i am saving the combined bitmap (now in loadedWork) to file as follows:
try {
final FileOutputStream out = new FileOutputStream(new File("/sdcard/SoftCopy" + "/mybitmap.png"));
loadedWork.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
return true;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
THE problem is that the combined bitmap(loadedWork) gets saved as png file for 1st time and i am able to load it, however WHEN I AGAIN TRY TO SAVE AFTER MAKING SOME MODIFICATIOns, then the application crashes. Can someone tell me how can I be able to resave the combined bitmap.