我已经研究了许多不同的方法来创建图像的缩小位图,但是它们都不能正常工作/我需要一些不同的方法。
这有点难以解释:-)
我需要的是一个保持图片比例的位图,但小于某个大小 - 例如 1mb 或像素尺寸的等效值(因为此位图需要添加为 putExtra() 以实现意图)。
到目前为止我遇到的问题:
我看过的大多数方法都创建了位图的缩放版本。所以:图像 -> Bitmap1(未缩放)-> Bitmap2(缩放)。但是如果图像的分辨率非常高,则它的按比例缩小不足。我认为解决方案是创建一个精确大小的位图,以便可以充分降低任何分辨率。
但是,这种方法的副作用是已经小于所需大小的图像将被调整大小(或者调整大小不起作用?)。所以需要有一个“if”来检查图像是否可以在不调整大小的情况下转换为位图。
我不知道如何去做,所以非常感谢任何帮助!:-)
这就是我目前正在使用的(我不想这样做):
// This is called when an image is picked from the gallery
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
selectedImage = imageReturnedIntent.getData();
viewImage = imageReturnedIntent.getData();
try {
decodeUri(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iv_preview.setImageBitmap(mImageBitmap);
}
break; // The rest is unnecessary
这是当前正在缩放大小的部分:
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true; //
BitmapFactory.decodeStream(getActivity().getContentResolver()
.openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 260; // Is this kilobites? 306
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o2.inScaled = false; // Better quality?
mImageBitmap = BitmapFactory.decodeStream(getActivity()
.getContentResolver().openInputStream(selectedImage), null, o2);
return BitmapFactory.decodeStream(getActivity().getContentResolver()
.openInputStream(selectedImage), null, o2);
}
如果有什么需要解释的,请说。
谢谢