Search code examples
androidresizejpegwatermark

Android - Resizing and adding watermark to photo delivers poor quality image


This is an Android question:

I am taking a jpeg picture taken with the camera and resizing it to 640 x 480 resolution after which I add a watermark to the image. This bitmap is then compressed to a jpeg file and saved.

The problem is that the quality of the jpeg is reduced considerable as you can see from the examples below. The first one is the original jpeg taken by the camera and the second one is the resized and watermarked version. You can see the graininess and colour inconsistencies introduced in the final image.

Also as a separate question, is there a way to reduce the size of the final jpeg image? My Samsung phone on 640x480 resolution delivers a jpeg of about 100KB, whereas using the code below I get a final jpeg size of ~250KB. Reducing "Bitmap.CompressFormat.JPEG" quality to 70% does not get me even close to 100KB.

See the code below.

Source image form camera: http://www.freeimagehosting.net/ehtso

Final resized and watermarked image: 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();
    }
}

Solution

  • It seems that I am not able to improve the quality of the images, nor am I able to reduce the size and keep the quality in tact. This is the limitations of Android.