【发布时间】:2014-03-19 16:53:52
【问题描述】:
我有一个在 Android 4.0 及更高版本上开发的应用程序。 (该应用不支持4.0以下的Android版本[Ice Cream Sandwich])。
该问题与各种图像(例如 jpeg 或 png )格式的(打印)DPI 有关。
此问题与屏幕 DPI 或各种 Android 设备的尺寸无关。它也与在设备上以屏幕大小显示位图无关。
我正在使用以下代码在“位图”中加载图像文件。然后我一直在裁剪它并使用 jpegCompression 将其保存到另一个 JPEG 格式的文件中。我已经可以通过以下代码做到这一点,但是我无法获取加载的 DPI 或设置保存的图像文件的 DPI。
所以我有两个问题。
1) 如何从 JPEG 文件中获取(打印)DPI,在将其加载到“位图”之后或同时?
2) 在保存新生成的“位图”时,如何在 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 和其他网站中找到,但无法获得正确的相关答案。所以我决定在这个论坛问它。
欢迎您的提示和建议。
桑杰。
【问题讨论】:
标签: android image printing bitmap dpi