在我的Android应用程序中,当我从相机捕获图像时,我想重新调整它的尺寸。当我从画廊拍摄图像时,我成功地重新调整尺寸。但是当我从相机捕捉时,我失败了。请帮忙
if (requestCode == take_image && resultCode == RESULT_OK && null != data) {
thumbnail = (Bitmap) data.getExtras().get("data");
image2 = me1;
addattachmentsToListView(image2);
}
以下是从sdcard调整图像大小的代码:
if (requestCode == UploadFile && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
try {
Bitmap image=(decodeUri(selectedImage));
addattachmentsToListView(image);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 200;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 1.5 < REQUIRED_SIZE || height_tmp / 1.5 < REQUIRED_SIZE) {
break;
}
width_tmp /= 4;
height_tmp /= 4;
// width_tmp = 20;
// height_tmp = 20;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o2);
}
答案 0 :(得分:0)
在public void onPreviewFrame(byte[] data, Camera camera)
中,您可以通过这种方式获得位图:
Bitmap bmp = YUVtoBMP(data, ImageFormat.NV21, YOUR_CAMERA_SIZE);
public Bitmap YUVtoBMP(byte[] data, int format, Camera.Size size)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(data, format, size.width, size.height, null);
yuvImage.compressToJpeg(new Rect(0, 0, size.width, size.height), 50, out);
byte[] imageBytes = out.toByteArray();
Options options = new Options();
options.inPreferQualityOverSpeed = false;
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options);
return image;
}
为了缩放位图,您可以:
Bitmap scaled = Bitmap.createScaledBitmap(bmp, NEW_WIDTH, NEW_HEIGHT, true);
这只是一个提示:从此开始,尝试寻找优化(如果有的话)。