【问题标题】:How to make high resolution image如何制作高分辨率图像
【发布时间】:2022-03-17 12:06:56
【问题描述】:

我制作了一个使用过滤器图像并保存在 sdcard 中的应用程序,它运行良好,但我想以高低两种分辨率保存图像,我从未尝试过分辨率,任何人都可以帮助了解如何保存这两个分辨率?我的代码如下

private void saveBitmap(Bitmap bmp, String fileName, int resolution, String resolutionQuality) {
    // File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + fileName + ".png");
    File f = new File(Environment.getExternalStorageDirectory() + "FiltureImages");
    if (!f.exists()) {
        File wallpaperDirectory = new File("/sdcard/FiltureImages/");
        wallpaperDirectory.mkdirs();
    }
    File file = new File(new File("/sdcard/FiltureImages/"), fileName + resolutionQuality + ".png");
    if (file.exists()) {
        file.delete();
    }
    try {
        fos = new FileOutputStream(file);

        bmp.compress(Bitmap.CompressFormat.PNG, resolution, fos);
        Toast.makeText(mActivity, "Image save successfully", Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: android image


    【解决方案1】:

    像这样打电话saveBitmap(bm, "high", "low", 100, 0, "quality");

    质量范围从0 -100

    注意:Bitmap.CompressFormat.PNG 更改为Bitmap.CompressFormat.JPEG,因为质量适用于JPEG 格式。

    private void saveBitmap(Bitmap bmp, String fileNameHigh, String fileNameLow, int resolutionHigh, int resolutionLow, String resolutionQuality) {
    
            File f = new File(Environment.getExternalStorageDirectory() + "FiltureImages");
            if (!f.exists()) {
                f.mkdirs();
            }
            File file = new File(f, fileNameHigh + resolutionQuality + ".png");
            if (file.exists()) {
                file.delete();
            }
            File file1 = new File(f, fileNameLow + resolutionQuality + ".png");
            if (file1.exists()) {
                file1.delete();
            }
            try {
                FileOutputStream high = new FileOutputStream(file);
                FileOutputStream low = new FileOutputStream(file1);
    
                bmp.compress(Bitmap.CompressFormat.JPEG, resolutionHigh, high);
                bmp.compress(Bitmap.CompressFormat.JPEG, resolutionLow, low);
                Toast.makeText(this, "Image save successfully", Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(this, "Image save falid", Toast.LENGTH_SHORT).show();
            }
        }
    

    【讨论】:

    • @Komal 编码太棒了 :)
    【解决方案2】:

    以这种方式创建具有备用分辨率的第二个位图并将scaledBmp.compress() 调用到一个新文件中。

            Bitmap scaledBmp = Bitmap.createScaledBitmap(bmp, newWidth, newHeight, false);
    

    【讨论】:

      猜你喜欢
      • 2014-11-20
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2017-08-23
      • 2017-11-26
      • 1970-01-01
      • 2011-04-07
      相关资源
      最近更新 更多