拍摄的图像 returns 小尺寸

Captured image returns small size

我正在从相机捕捉图像。捕获的图像尺寸在捕获时显示太小。但稍后,如果我在图库中查看,捕获的图像大小以 MB 为单位显示。

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

为什么拍摄时显示的图片尺寸变小了?

      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) {

        }
    }

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

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

编辑:

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

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

您正在使用 缩略图 而不是实际图像。

要获取实际图像,您必须将图像文件 uri 作为 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了解所需的步骤和完整代码。


从文件路径

获取缩放的Bitmap
    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);