簡體   English   中英

在Android中將位圖的大小減小到某些指定像素

[英]Reduce size of Bitmap to some specified pixel in Android

我想將我的位圖圖像大小減小到最大640px。 例如,我有大小為1200 x 1200像素的位圖圖像。如何將其縮小為640像素。

如果通過位圖的widthheight使用:

public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) {
    return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true);
}

如果要保持位圖比例不變,但將其減小到最大邊長,請使用:

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float) width / (float) height;
        if (bitmapRatio > 1) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }

        return Bitmap.createScaledBitmap(image, width, height, true);
}

使用這個方法

 /** getResizedBitmap method is used to Resized the Image according to custom width and height 
  * @param image
  * @param newHeight (new desired height)
  * @param newWidth (new desired Width)
  * @return image (new resized image)
  * */
public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}

或者您可以這樣做:

Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);

通過filter = false將導致塊狀像素化圖像。

通過filter = true將使您的邊緣更平滑。

這是將位圖圖像分辨率(像素)降低到所需值的有效代碼...

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageProcessActivity extends AppCompatActivity {


    private static final String TAG = "ImageProcessActivity";
    private static final String IMAGE_PATH = "/sdcard/DCIM/my_image.jpg";

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {

                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    public static Bitmap decodeSampleDrawableFromFile(String file, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(file, options);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_process);

        new AsyncTask<Object, Object, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Object... objects) {

                try {

                    return decodeSampleDrawableFromFile(IMAGE_PATH, 640, 640);

                } catch (Exception e) {

                    e.printStackTrace();

                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);

                ((ImageView) findViewById(R.id.img)).setImageBitmap(bitmap);
            }
        }.execute();
    }
}

腳步:

  1. 獲取Bitmap.Options (圖像信息)。

  2. 將樣本大小調整為所需樣本大小。

  3. 負載Bitmap具有給定的選項(期望的分辨率)從圖像文件轉換成bitmap對象。 但是,請在后台線程中執行此操作。

  4. Bitmap圖像加載到UI線程( onPostExecute() )上的ImageView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM