繁体   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