Android - 如何在以编程方式加载或保存 JPEG 文件时获取或设置(打印)DPI(每英寸点数)? [英] Android - how to get or set (print) DPI ( dots per inch ) of JPEG file while loading or saving it programmatically?

查看:35
本文介绍了Android - 如何在以编程方式加载或保存 JPEG 文件时获取或设置(打印)DPI(每英寸点数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Android 4.0 及更高版本上开发的应用.(该应用不支持 Android 4.0 以下版本 [Ice Cream Sandwich]).

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] ).

该问题与各种图像(例如 jpeg 或 png )格式的(打印)DPI 相关.

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

这个问题与屏幕 DPI 或各种 Android 设备的尺寸无关.它也与以屏幕尺寸在设备上显示位图无关.

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.

我正在使用以下代码在位图"中加载图像文件.然后我一直在裁剪它并使用 jpegCompression 将它保存到另一个 JPEG 格式的文件中.我已经能够通过以下代码做到这一点,但我无法获取加载的 DPI 或设置保存的图像文件的 DPI.

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.

所以我有两个问题.

1) 在位图"加载之后或加载时,如何从 JPEG 文件中获取(打印)DPI?

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

2) 在保存新生成的位图"时,如何在 JPEG 文件中再次设置 DPI?

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;

我试图通过谷歌搜索从 stackoverflow 和其他站点中查找,但无法获得正确的相关答案.所以我决定在这个论坛提问.

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.

欢迎您的提示和建议.

桑杰.

推荐答案

这里为了方便起见准备了复制粘贴方法:

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);
}

保存的文件将正确设置图像 DPI 值:

saved file will have properly set Image DPI value:

这篇关于Android - 如何在以编程方式加载或保存 JPEG 文件时获取或设置(打印)DPI(每英寸点数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆