0

i am try to make resize image but when resize to get low resolution image. is there other solution to image resize in andorid using java code.

       BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
          Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
            int h = 300;
            int w = 300;
            Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, h, w, true);

            String root = Environment.getExternalStorageDirectory()
                    .toString();
            File myDir = new File(root + "/upload_images");
            myDir.mkdirs();
            String fname = null;
            if (rname == null) {
                fname = "Image.jpg";
            } else {
                fname = rname + ".jpg";
                Log.i("log_tag", "File anem::" + fname);
            }
            file = new File(myDir, fname);
            Log.i("log_tag", "" + file);
            if (file.exists())
                file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                scaled.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
  • What's not working with this version, you aren't clear on that. But you're shrinking it and compressing to JPEG (rather than lossless PNG), so you're going to take a big quality hit. – Gabe Sechan Mar 22 '13 at 07:09
  • refer this...guess it will be helpful http://stackoverflow.com/questions/4231817/quality-problems-when-resizing-an-image-at-runtime?rq=1 – karan Mar 22 '13 at 07:10

1 Answers1

0

change this line:

scaled.compress(Bitmap.CompressFormat.JPEG, 100, out);

to

scaled.compress(Bitmap.CompressFormat.JPEG, 90, out);

and I ll saggest you below Method for compress image BITMAP.

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

for more Detail Check this : https://stackoverflow.com/a/823966/1168654

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177