减少位图大小
问题描述:
我有这台机器,我必须捕获产品的图像并填充其他数据以将它们上载到服务器。我一直在使用图像捕捉意图,但系统将图像缩小到150 * 200,但感谢您现在我拥有全尺寸图像。 问题是图像的大小是一个大的(2-5mbs)我试图缩小位图方法和毕加索图书馆,但图像文件仍然具有相同的大小。你能给我指导如何减少它的大小(如果需要的话甚至是分辨率)。 我的尝试:减少位图大小
public void grabImage(ImageView imageView) {
this.getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
Bitmap bitmap;
try
{
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
//ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Bitmap bm = ShrinkBitmap(fullpath, bitmap.getWidth(), bitmap.getHeight());
imageView.setImageBitmap(bm);
}
catch (Exception e)
{
Log.d("This", "Failed to load", e);
}
}
Bitmap ShrinkBitmap(String file, int width, int height){
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = 4;
} else {
bmpFactoryOptions.inSampleSize = 4;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
问候,
答
以下网站非常清楚地解释了如何以高效的方式加载图像。这意味着,在加载图片时,您已经以2(2,4,8,...)的最高次幂进行缩放,以使加载的图片仍然大于所需的大小。这样你只需加载你实际需要的东西,并使用更少的内存。
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
(我看你使用此过程的一些元素在你的代码,但它没有意义对我来说)
在第二个步骤,那么你可以进一步缩小图片的实际宽度+ heigth你需要,这只需要完成: Bitmap.createScaledBitmap(bitmap_large,(int)width,(int)height,false);
答
试试这个;
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize *=2;
}
else {
bmpFactoryOptions.inSampleSize = 4;
}
}
显示你的尝试。 – iTurki
[如何在上载到服务器之前减少图像文件大小]的可能重复(http://*.com/questions/18573774/how-to-reduce-an-image-file-size-before-uploading-to -a服务器) –