捕获的图像返回小尺寸 [英] Captured image returns small size

查看:187
本文介绍了捕获的图像返回小尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从相机中捕捉图像。拍摄时捕获的图像尺寸显示得太小。但是后来如果我在图库中检查捕获的图像大小以MB显示。

I am capturing the image from camera. The captured image's size shows too small when captured. But later if I check in gallery the captured image size shows in MB.

我尝试调试代码,所以在调试时我检查了图像捕获后的文件长度length显示26956个字节,当我在gallery中检查相同的图像时,图像的大小为1.3 MB。

I tried debugging the code, so while debugging I checked length of the file after image is captured the length shows 26956 bytes, and when I checked same image in gallery the size of the image is 1.3 MB.

为什么拍摄时图像尺寸会减少?

Why the image size shows reduced when captured?

      private void cameraIntent() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_CAMERA);

    }

     private void onCaptureImageResult(Intent data) {

        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

 (thumbnail.getWidth()/2),(int)(thumbnail.getHeight()/2),true);

        thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".png");

        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        long size = destination.length();// here size of the image is too small

        selectFile = false;

        loadImageFromFile(destination.getAbsolutePath());

    }


      public void loadImageFromFile(String imageFile) {

        try {
            ExifInterface ei = new ExifInterface(imageFile);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_UNDEFINED);

            Bitmap bitmap = BitmapFactory.decodeFile(imageFile);

            Bitmap rotatedBitmap = null;

            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotatedBitmap = rotateImage(bitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotatedBitmap = rotateImage(bitmap, 180);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotatedBitmap = rotateImage(bitmap, 270);
                    break;
                case ExifInterface.ORIENTATION_NORMAL:
                    rotatedBitmap = bitmap;
                    break;
                default:
                    rotatedBitmap = bitmap;
                    break;
            }

            if (rotatedBitmap != null) {

                if (selectFile && fileSizeInKB > 500) {
                    rotatedBitmap = Bitmap.createScaledBitmap(rotatedBitmap, (int) (rotatedBitmap.getWidth() * 0.3), (int) (rotatedBitmap.getHeight() * 0.3), true);
                }

                else if(selectFile && fileSizeInKB > 1024){

                    rotatedBitmap = Bitmap.createScaledBitmap(rotatedBitmap, (int) (rotatedBitmap.getWidth() * 0.2), (int) (rotatedBitmap.getHeight() * 0.2), true);
                }
                else if(selectFile && fileSizeInMB > 2){

                    rotatedBitmap = Bitmap.createScaledBitmap(rotatedBitmap, (int) (rotatedBitmap.getWidth() * 0.1), (int) (rotatedBitmap.getHeight() * 0.1), true);
                }

                profileImageView.setImageBitmap(rotatedBitmap);
                selectedBitmap = rotatedBitmap;

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                selectedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
                byte[] byteArray = stream.toByteArray();

                File tempFile = File.createTempFile("temp", null, getCacheDir());
                FileOutputStream fos = new FileOutputStream(tempFile);
                fos.write(byteArray);

                Long size = tempFile.length();

                profileImage = tempFile;
            }

        } catch (IOException ex) {

        }
    }

我正在缩放从图库中选择的图像,我也想缩放从相机中捕获的图像,但是图像的大小我不合适。

I am scaling the images selected from gallery, I too want to scale the images captured from camera, but the size of the image I am not getting appropriate.

有人可以帮忙吗?谢谢......

Can anyone help for this please? Thank you...

编辑:

     private void cameraIntent() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go

            try {
                photoFile = createImageFile();
            } catch (IOException ex) {

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                startActivityForResult(intent, REQUEST_CAMERA);
            }
        }

    }



     private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "image";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        fileName = image.getAbsolutePath();
        return image;
    }

     private void onCaptureImageResult(Uri data) {

        try {

            Bitmap thumbnail = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data);


            selectFile = false;

            long fileSizeInBytes = photoFile.length();
// Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
            fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
            fileSizeInMB = fileSizeInKB / 1024;

            loadImageFromFile(photoFile.getAbsolutePath());

        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

 public void loadImageFromFile(String imageFile) {

    try {
        ExifInterface ei = new ExifInterface(imageFile);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap bitmap = BitmapFactory.decodeFile(imageFile);

        Bitmap rotatedBitmap = null;

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotatedBitmap = rotateImage(bitmap, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotatedBitmap = rotateImage(bitmap, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotatedBitmap = rotateImage(bitmap, 270);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
                rotatedBitmap = bitmap;
                break;
            default:
                rotatedBitmap = bitmap;
                break;
        }

        if (rotatedBitmap != null) {
            //

            if (selectFile && fileSizeInMB < 1 && fileSizeInKB > 500) {
                rotatedBitmap = Bitmap.createScaledBitmap(rotatedBitmap, (int) (rotatedBitmap.getWidth() * 0.9), (int) (rotatedBitmap.getHeight() * 0.9), true);
            }

            else if(selectFile && fileSizeInMB < 2){

                rotatedBitmap = Bitmap.createScaledBitmap(rotatedBitmap, (int) (rotatedBitmap.getWidth() * 0.3), (int) (rotatedBitmap.getHeight() * 0.3), true);
            }
            else if(selectFile && fileSizeInMB > 2){

                rotatedBitmap = Bitmap.createScaledBitmap(rotatedBitmap, (int) (rotatedBitmap.getWidth() * 0.2), (int) (rotatedBitmap.getHeight() * 0.2), true);
            }
            else if(selectFile && fileSizeInMB > 3){

                rotatedBitmap = Bitmap.createScaledBitmap(rotatedBitmap, (int) (rotatedBitmap.getWidth() * 0.1), (int) (rotatedBitmap.getHeight() * 0.1), true);
            }
            //  resize(rotatedBitmap,bitmap.getWidth()/2,bitmap.getHeight()/2);

            profileImageView.setImageBitmap(rotatedBitmap);
            selectedBitmap = rotatedBitmap;

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            selectedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
            byte[] byteArray = stream.toByteArray();

            File tempFile = File.createTempFile("temp", null, getCacheDir());
            FileOutputStream fos = new FileOutputStream(tempFile);
            fos.write(byteArray);

            Long size = tempFile.length();

            profileImage = tempFile;
        }

    } catch (IOException ex) {
        //  UiUtils.showAlert(getString(R.string.error),NewGroupAcvitity.this);
    }
}

现在使用此代码,当我捕获图像后捕获它需要时间加载图像视图并显示空白屏幕,直到图像设置为图像视图。

Now with this code, when I capture the image after capturing it takes time to load on image view and show blank screen till the image is set to the image view.

推荐答案

你是使用缩略图而不是实际图像。

You are using the Thumbnail instead of the actual image.

要获取实际图像,您必须将图像文件uri传递给相机意图 MediaStore.EXTRA_OUTPUT

To get the actual image you have to pass Image file uri to the Camera intent as MediaStore.EXTRA_OUTPUT

示例:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);//photoURI - file uri where you want the image to be saved
startActivityForResult(intent, REQUEST_CAMERA);

参考 https://developer.android.com/training/camera/photobasics.html#TaskPath 获取所需步骤和完整代码。

Refer https://developer.android.com/training/camera/photobasics.html#TaskPath for the required steps and complete code.

从文件路径获取缩放位图

    int targetW = 800;
    int targetH = 1000;

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    // Decode the image file into a Bitmap
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);

这篇关于捕获的图像返回小尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆