获取图像较小尺寸的摄像头的Andr​​oid拍摄的时候较小、摄像头、图像、尺寸

2023-09-04 03:26:05 作者:将爱意深埋

在我当我捕捉图像从相机的Andr​​oid应用程序,我想重新大小它的尺寸。我成功了重新大小,当我把图像从画廊。但是,当我从相机捕获,我失败了。请帮忙

 如果(要求code == take_image和放大器;&安培;结果code == RESULT_OK和放大器;&安培;空=数据!){
    。缩略图=(位图)data.getExtras()获得(数据);
    IMAGE2 = ME1;
    addattachmentsToListView(IMAGE2);
}
 

下面是code,从SD卡调整图像:

 如果(要求code == UploadFile和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!空=数据){
    乌里selectedImage = data.getData();
    尝试 {

        位图图像=(德codeURI(selectedImage));
        addattachmentsToListView(图像);
        }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
}


私人位图德codeURI(URI selectedImage)抛出FileNotFoundException异常{
    BitmapFactory.Options O =新BitmapFactory.Options();
    o.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeStream(
    。getContentResolver()openInputStream(selectedImage),空,O);

    最终诠释REQUIRED_SIZE = 200;

    INT width_tmp = o.outWidth,height_tmp = o.outHeight;
    int标= 1;
    而(真){
        如果(width_tmp / 1.5安培; LT; REQUIRED_SIZE || height_tmp / 1.5安培; LT; REQUIRED_SIZE){
            打破;
        }
        width_tmp / = 4;
        height_tmp / = 4;

        // width_tmp = 20;
        // height_tmp = 20;
        规模* = 2;
    }

    BitmapFactory.Options O2 =新BitmapFactory.Options();
    o2.inSampleSize =规模;
    返回BitmapFactory.de codeStream(
    getContentResolver()openInputStream(selectedImage),空,O2)。
}
 

解决方案

在你的在previewFrame公共无效(byte []的数据,摄像头摄像头),你可以得到一个位图是这样的:

  BMP位= YUVtoBMP(数据,ImageFormat.NV21,YOUR_CAMERA_SIZE);

公共位图YUVtoBMP(byte []的数据,INT格式,Camera.Size大小)
{
    ByteArrayOutputStream OUT =新ByteArrayOutputStream();
    YuvImage yuvImage =新YuvImage(数据,格式,size.width,size.height,NULL);
    yuvImage.com pressToJpeg(新的矩形(0,0,size.width,size.height),50,出);
    byte []的imageBytes = out.toByteArray();
    选项​​选项=新的选项();
    options.in preferQualityOverSpeed​​ = FALSE;
    位图图像= BitmapFactory.de codeByteArray(imageBytes,0,imageBytes.length,期权);

    返回形象;
}
 

为了扩展位图,您可以:

 位图缩放= Bitmap.createScaledBitmap(BMP,NEW_WIDTH,NEW_HEIGHT,真正的);
 

这仅仅是一个暗示:从这个开始,并尝试寻找优化,如果任何

In my android application when I capture image from camera, I want to re-size it's dimensions. I am successful to re-size, when I take image from gallery. But when I capture from camera, I failed. Please help

if (requestCode == take_image &&  resultCode == RESULT_OK && null != data) {
    thumbnail = (Bitmap) data.getExtras().get("data");  
    image2 = me1;
    addattachmentsToListView(image2);
}

Here is the code to resize image from 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);
}

解决方案

In your public void onPreviewFrame(byte[] data, Camera camera), you could get a bitmap in this way:

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

In order to scale a bitmap, you can:

Bitmap scaled = Bitmap.createScaledBitmap(bmp, NEW_WIDTH, NEW_HEIGHT, true);

This is just a hint: start from this and try to look for optimization if any.

 
精彩推荐