簡體   English   中英

如何將位圖從相機的10mb圖像壓縮到300kb beforw設置為Android中的imageview

[英]How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android

我有一些代碼:

Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions); 

最終,位圖具有來自相機的圖像(我正在從高分辨率相機拍攝圖像),因此大小可能為10mb。


我正在嘗試做的事情:

我正在嘗試在imageview中設置位圖,如下所示

portfolioPicImgId.setImageBitmap(bitmap);
  • 在此之前,我想將圖像壓縮到300kb,我該如何實現!
  • 我看到了其他stackoverflow答案,但如何確切地指定為300kb

很難准確獲得300kb。 這取決於質量,每個像素包含4(32位)或2(16位)字節,這意味着圖像應包含75000像素(在32位圖像中)。 下一步,為簡單起見,將圖像比例取為1:1的圖像比例-sqrt(75000)〜274像素,我們得到274x274像素在ram中約為300kb。

您需要處理的所有數據都在選項中指定了寬度和高度。

你有嘗試過嗎?

//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;
}

只需在下面的函數中傳遞圖像的路徑

    public Bitmap get_Reduce_bitmap_Picture(String imagePath) {

    int ample_size = 16;
     // change ample_size to 32 or any power of 2 to increase or decrease bitmap size of image


    Bitmap bitmap = null;
    BitmapFactory.Options bitoption = new BitmapFactory.Options();
    bitoption.inSampleSize = ample_size;

    Bitmap bitmapPhoto = BitmapFactory.decodeFile(imagePath, bitoption);

    ExifInterface exif = null;
    try {
        exif = new ExifInterface(imagePath);
    } catch (IOException e) {
        // Auto-generated catch block
        e.printStackTrace();
    }
    int orientation = exif
            .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    Matrix matrix = new Matrix();

    if ((orientation == 3)) {
        matrix.postRotate(180);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else if (orientation == 6) {
        matrix.postRotate(90);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else if (orientation == 8) {
        matrix.postRotate(270);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    } else {
        matrix.postRotate(0);
        bitmap = Bitmap.createBitmap(bitmapPhoto, 0, 0,
                bitmapPhoto.getWidth(), bitmapPhoto.getHeight(), matrix,
                true);

    }

    return bitmap;

}

暫無
暫無

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

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