簡體   English   中英

從Android中的相機捕獲圖像時,圖像尺寸變小

[英]Getting smaller size of image when captured from camera in Android

在我的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);
}

在您的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);

這只是一個提示:從此開始並嘗試尋找優化(如果有)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM