Android - 调整照片大小和添加水印会导致图像质量不佳

时间:2012-03-08 14:17:01

标签: android resize jpeg watermark

这是一个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();
    }
}

2 个答案:

答案 0 :(得分:0)

似乎我无法提高图像的质量,也无法缩小尺寸并保持质量。这是Android的限制。

答案 1 :(得分:0)

在我的项目中,我也希望为照片制作水印。在读完你的照片之后,我项目中“内存不足”的问题已得到修复。谢谢。 O(∩_∩)O〜