我正在开发一个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);
答案 0 :(得分:5)
使用这个:
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;
}
答案 1 :(得分:0)
您可以使用Bitmap.compress()
生成压缩版本。