我正在用android开发一个应用程序,它将捕获照片并存储在sqlite中。
图像大小为70 kb,但我想以35 kb的大小存储该图像。但是我对此一无所知。我尝试了以下代码,但没有成功。

int photo_width = bm.getWidth();
int photo_height = bm.getHeight();

photo_width = 260;
photo_height = 260;

Bitmap photobitmap = Bitmap.createScaledBitmap(bm,
    photo_width, photo_height, false);

最佳答案

使用这个:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);

    return resizedBitmap;
}


您可以使用这个...这是最好的例子。

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

关于android - 在android中,如何通过编程将我的图片大小从70 kb减小到30 kb?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20262590/

10-12 02:08