我开发了一个 Android 应用程序,支持 Android 4.0 及以上版本(该应用程序不支持 Android 4.0 [冰激凌三明治] 版本以下的设备)。
这个问题与不同图像 (例如 jpeg 或 png) 格式的 (打印) DPI 相关。
此问题与 Android 设备的屏幕 DPI 或尺寸无关。它也与在设备屏幕大小上显示位图无关。
我正在使用以下代码加载图像文件到 'Bitmap' 中,然后将其裁剪并以 jpeg 压缩格式保存到另一个文件中。 我已经能够通过以下代码实现此操作,但我无法获得已加载或设置保存图像文件的 DPI。
所以,我有两个问题。
1)如何在加载 'Bitmap' 时从 JPEG 文件中获取(打印)DPI?
2)在保存新生成的 'Bitmap' 时,如何再次设置 JPEG 文件的 DPI?
以下是参考代码片段:
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;
我尝试从stackoverflow和其他网站上通过谷歌搜索找到了答案,但是没有得到相关的正确答案。因此,我决定在这个论坛上问一下。
欢迎您提出任何提示和建议。
Sanjay。