【问题标题】:Android - Resizing and adding watermark to photo delivers poor quality imageAndroid - 为照片调整大小和添加水印会导致图像质量不佳
【发布时间】:2012-03-26 00:24:00
【问题描述】:

这是一个 Android 问题:

我正在拍摄一张用相机拍摄的 jpeg 图片,并将其调整为 640 x 480 分辨率,然后在图片中添加水印。然后将此位图压缩为 jpeg 文件并保存。

问题在于 jpeg 的质量大大降低,您可以从下面的示例中看到。第一个是相机拍摄的原始jpeg,第二个是调整大小和水印的版本。您可以看到最终图像中引入的颗粒感和颜色不一致。

另外作为一个单独的问题,有没有办法减小最终 jpeg 图像的大小?我的三星手机在 640x480 分辨率下提供约 100KB 的 jpeg,而使用下面的代码,我得到的最终 jpeg 大小约为 250KB。将“Bitmap.CompressFormat.JPEG”质量降低到 70% 甚至不会让我接近 100KB。

请看下面的代码。

源图片形式摄像头:http://www.freeimagehosting.net/ehtso

最终调整大小和水印的图像:http://www.freeimagehosting.net/qsxs6

public class AIS_PhotoResize {

//resize photos to reduce data cost for sending photos
public String ResizePhoto(String photoName)
{       
    //folder location for existing photos
    File oldFileRoot = new File(Environment.getExternalStorageDirectory() + File.separator + "AIS_Photos" + File.separator);
    File oldFile = new File(oldFileRoot, photoName);

    //folder location for resized photos
    File newFileRoot = new File(Environment.getExternalStorageDirectory() + File.separator + "AIS_Photos" + File.separator + "Resized" + File.separator);
    //ensure folder exist
    newFileRoot.mkdirs();
    File newFile = new File(newFileRoot, photoName);

    Bitmap b = null;
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(oldFile);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        //The new size we want to scale to
        final int IMAGE_MAX_SIZE=800;

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;     
        o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
        o2.inScaled = false;
        o2.inDither = true;
        fis = new FileInputStream(oldFile);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();

        //scale resized bitmap (version 1 - scaling using createScaledBitmap) 
        int scaledHeight = (int) (640 / ((float)b.getWidth() / (float)b.getHeight()));
        Bitmap bScaled = Bitmap.createScaledBitmap(b, 640, scaledHeight, true);
        b.recycle();

        /*
        //scale resized bitmap (version 2 - scaling using matrix)            
        Matrix matrix = new Matrix();
        float desiredScale = 640 / (float)b.getWidth();
        matrix.postScale(desiredScale, desiredScale);
        Bitmap bScaled = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
        //b.recycle();
        */

        //add timestamp watermark to photo
        String watermark = photoName.substring(0, photoName.length() - 4);
        Bitmap dest = Bitmap.createBitmap(bScaled.getWidth(), bScaled.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas cs = new Canvas(dest);
        Paint tPaint = new Paint();
        tPaint.setTextSize(15);
        tPaint.setColor(Color.RED);
        tPaint.setStyle(Style.FILL);
        tPaint.setAntiAlias(true);
        Paint bScaledPaint = new Paint();
        bScaledPaint.setDither(true);
        cs.drawBitmap(bScaled, 0f, 0f, bScaledPaint);
        float height = tPaint.measureText("yY");
        cs.drawText(watermark, 5f, height+5f, tPaint);
        bScaled.recycle();

        //encode scaled bitmap to jpeg
        FileOutputStream out = new FileOutputStream(newFile);
        dest.compress(Bitmap.CompressFormat.JPEG, 100, out);

        //cleanup
        dest.recycle();
        out.close();

        return "Success";

    } catch (IOException e) {
        ErrorReporter.getInstance().handleException(e);
        return "Error: " + e.toString();
    }
}

【问题讨论】:

  • a) 你缩放两次 -> 质量损失 b) 抖动可能很糟糕,如果它是 ARGB_8888,则不需要。当你保存它们时,你检查过中间步骤的外观吗(添加一些b.compress() 步骤 - 最好是 PNG 以检查质量)?
  • 那不是水印。你不应该看到水印。

标签: android resize jpeg watermark


【解决方案1】:

似乎我无法提高图像的质量,也无法减小尺寸并保持质量不变。这是安卓的局限。

【讨论】:

    【解决方案2】:

    在我的项目中,我也想给照片做水印。看了你的之后,我的项目中“内存不足”的问题已经解决了。非常感谢。 O(∩_∩)O~~

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      相关资源
      最近更新 更多