android 更改位图图像的 dpi canvas

android Change the dpi of a Bitmap image canvas

在我的代码中,我有一个转换位图的图像,更改大小并分配一个 setDensity。然后我保存在sd.

在此示例中,最终尺寸为 5x7 英寸

这是我的代码:

private void SaveImage(Bitmap myBitmap) {

        int TARGET_DENSITY = 300;
        int PHYSICAL_WIDTH_IN_INCH = 5;
        int PHYSICAL_HEIGHT_IN_INCH = 7;

        Bitmap bmp = Bitmap.createBitmap(PHYSICAL_WIDTH_IN_INCH * TARGET_DENSITY, PHYSICAL_HEIGHT_IN_INCH * TARGET_DENSITY, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bmp);
        canvas.drawBitmap(myBitmap, 0, 0, new Paint());

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/aaa");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            bmp.setDensity(TARGET_DENSITY);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
            Log.i("control","Error");
        }
    }

图片原尺寸为w:1024 x h:768px - 300dpi

应用程序生成的图像的最终尺寸为:w:1500 x h:2100px - 72dpi

如果用photoshop把72dpi改成300dpi,结果是正确的(5x7inches),但是图片是72dpi输出的

有什么方法可以从应用程序生成 300dpi 的图像吗?

我正在寻找信息,但没有成功。

提前致谢

你的形象没有问题。它是 1500 x 2100 像素,因此如果它在 300dpi 设备上呈现,图像将为 5"x7".

JPEG 格式不存储任何分辨率信息,所有内容均以像素为单位。 72dpi 仅来自 PhotoShop,它是将像素转换为英寸的默认分辨率设置。为什么是 72?您必须询问 PhotoShop 团队。

PhotoShop 允许您设置 DPI 分辨率,这样当您的水平和垂直标尺以英寸显示时,它们将根据您的目标输出设备显示正确的测量值。更改 DPI 不会更改图像的任何属性。