从Android中的位图保存dpi图像时如何设置? [英] How to set dpi image when saving it from bitmap in Android?

查看:462
本文介绍了从Android中的位图保存dpi图像时如何设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此功能将位图保存到sdcard上的文件中

I use this function to save bitmap to file on sdcard:

private static File storeImage(Context context, Bitmap image) {
    File pictureFile = getOutputMediaFile(context);
    if (pictureFile == null) {
        return null;
    }
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return pictureFile;
}

private static File getOutputMediaFile(Context context){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
            + "/Android/data/"
            + context.getPackageName()
            + "/Files");

    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
    File mediaFile;
    String mImageName="IMG_"+ timeStamp +".png";
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
    return mediaFile;
}

当我打开文件并查看图像DPI信息时,它显示72像素/英寸 像这样

When I open the file and see image DPI information, it show 72 pixels/inch like this

如何将其设置为300像素/英寸或其他值?

推荐答案

300 dpi非常适合打印,因此在设置图像的高度和宽度(以英寸为单位)时,考虑像素数,请乘以300. 例如喜欢创建位图

300 dpi is great for printing , so considering the pixel counts , when you set height and width (in inches) of image, multiply with 300. eg. lke on creating bitmap

Bitmap bm = Bitmap.createBitmap( W_inch*300  ,  H_inch*300  , Bitmap.Config.ARGB_8888);

在图像属性/细节上可能无法显示300dpi,但由于DPI是每1英寸线的点数,因此质量将与300dpi相同.

it may not show 300dpi on image properties/details but the quality will be same as 300 dpi since DPI is dots per 1 inch line.

这篇关于从Android中的位图保存dpi图像时如何设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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