Android - how to get or set (print) DPI ( dots per inch ) of JPEG file while loading or saving it programmatically?

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I have an app developed on Android versions 4.0 and above. ( The app does not support Android versions below 4.0 [Ice Cream Sandwich] ).

The question is related to (print) DPI of various images ( for eg. of jpeg or png ) format.

This question does NOT relate to SCREEN DPI or sizes of various Android devices. It is also NOT related to showing the Bitmap on the device in screen size.

I am using the following code to load the image file in 'Bitmap'. Then I have been cropping it and saving it to another file in JPEG format with jpegCompression. I have been able to do this by the following code, but I am unable to get DPI of loaded or set the DPI of saved Image file.

So I have two questions.

1) How can I get the (print) DPI from the JPEG file, after or while loading it in 'Bitmap'?

2) While saving the new generated 'Bitmap', how can I set the DPI again in the JPEG file?

Following is the part of code for reference.

    FileInputStream inputStream = new FileInputStream(theSourcePhotoFilePathName);      Bitmap bitmap = null;     BitmapRegionDecoder decoder = null;      BitmapFactory.Options options = new BitmapFactory.Options();             options.inSampleSize = 1;     options.inDensity = 300;   // Tried this but not working.      try {         decoder = BitmapRegionDecoder.newInstance(in, false);         bitmap = decoder.decodeRegion(region, options);      // the region has cropping coordinates.     } catch (IllegalArgumentException e){         Log.d("First Activity", "Failed to recycle bitmap for rect=" + region, e);     } catch (IOException e) {         Log.d("First Activity", "Failed to decode into rect=" + region, e);     } finally {         if (decoder != null) decoder.recycle();     }      inputStream.close();     inputStream = null;      FileOutputStream fos = new FileOutputStream( theTargetTempFolderDestFilePath );     bitmap.compress(CompressFormat.JPEG, jpegCompressionRatio, fos);     fos.flush();     fos.close();     fos = null;

I have tried to find from stackoverflow and from other sites by googling, but could not get the proper related answer. So I have decided to ask it in this forum.

Your hints and suggestions are welcome.

Sanjay.

回答1:

Just for convenience here is ready to copy and paste method:

public static void saveBitmapToJpg(Bitmap bitmap, File file, int dpi) throws IOException {     ByteArrayOutputStream imageByteArray = new ByteArrayOutputStream();     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, imageByteArray);     byte[] imageData = imageByteArray.toByteArray();      setDpi(imageData, dpi);      FileOutputStream fileOutputStream = new FileOutputStream(file);     fileOutputStream.write(imageData);     fileOutputStream.close(); }  private static void setDpi(byte[] imageData, int dpi) {     imageData[13] = 1;     imageData[14] = (byte) (dpi >> 8);     imageData[15] = (byte) (dpi & 0xff);     imageData[16] = (byte) (dpi >> 8);     imageData[17] = (byte) (dpi & 0xff); }

saved file will have properly set Image DPI value:



回答2:

Are you referring to the DPI metaData written as part of the JPEG file? I have struggled with that recently but came up with a solution. If you have already solved it, this answer can help others who run into the same problem.

When a Bitmap is compressed to JPEG in Android, it saves it in a JFIF segment format. Please see article here(

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!