0

我正在尝试调整图像大小,但是当调整大小以获得低分辨率图像时。有没有其他使用java代码在andorid中调整图像大小的解决方案。

       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();
4

1 回答 1

0

改变这一行:

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

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

我将在下面介绍压缩图像 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;
}

更多细节检查这个:https ://stackoverflow.com/a/823966/1168654

于 2013-03-22T07:15:01.987 回答