首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕获的图像返回小尺寸

捕获的图像返回小尺寸
EN

Stack Overflow用户
提问于 2016-12-08 11:10:07
回答 1查看 879关注 0票数 0

我正在用相机拍摄图像。捕获图像的大小在捕获时显示得太小。但是稍后,如果我签入图库,捕获的图像大小将以MB显示。

我试着调试代码,所以在调试过程中,在捕获图像之后,我检查了文件的长度,长度显示了26956字节,当我在图片库中检查相同的图像时,图像的大小为1.3MB。

为什么图像大小显示在捕获时会变小?

代码语言:javascript
复制
      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) {

        }
    }

我是缩放从画廊选择的图像,我也想缩放从相机拍摄的图像,但大小的图像,我是不合适的。

有人能帮忙吗?谢谢..。

编辑:

代码语言:javascript
复制
     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);
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-08 11:42:56

您使用的是缩略图,而不是实际图像。

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

样本:

代码语言:javascript
复制
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

代码语言:javascript
复制
    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);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41038027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档