【问题标题】:Choose image size in camera intent在相机意图中选择图像大小
【发布时间】:2019-04-07 06:33:34
【问题描述】:

我是这个论坛的新手,所以请多多包涵,如果有错误请轻轻指出, 所以我正在做一个项目,我正在将图像上传到服务器,现在我想限制图像的大小,我提供了一个“单击图像”选项,我的代码将打开默认相机意图并单击图片,或“从图库中选择”。 我的问题是关于“点击图片”,现在当用户点击图片时,我可以预设可以点击的图片最大尺寸吗?

【问题讨论】:

  • 您好,您所说的大小、分辨率或实际大小(以兆字节为单位)是什么意思?
  • 大小以MB为单位,但我猜两者是相互依赖的,降低分辨率,降低大小

标签: android android-studio android-camera-intent


【解决方案1】:

你可以在 onActivityResult 方法中调整图片大小,试试下面的代码 sn-p

 public static Bitmap handleSamplingAndRotationBitmap(Context context, Uri selectedImage)
            throws IOException {
        int MAX_HEIGHT = 1024;
        int MAX_WIDTH = 1024;
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream imageStream = context.getContentResolver().openInputStream(selectedImage);
        BitmapFactory.decodeStream(imageStream, null, options);
        imageStream.close();
        options.inSampleSize = calculateInSampleSizes(options, MAX_WIDTH, MAX_HEIGHT);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        imageStream = context.getContentResolver().openInputStream(selectedImage);
        Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);
        img = rotateImageIfRequired(context, img, selectedImage);
        return img;
    }

    private static int calculateInSampleSizes(BitmapFactory.Options options,
                                             int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee a final image
            // with both dimensions larger than or equal to the requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

            // This offers some additional logic in case the image has a strange
            // aspect ratio. For example, a panorama may have a much larger
            // width than height. In these cases the total pixels might still
            // end up being too large to fit comfortably in memory, so we should
            // be more aggressive with sample down the image (=larger inSampleSize).

            final float totalPixels = width * height;

            // Anything more than 2x the requested pixels we'll sample down further
            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }
        return inSampleSize;
    }
    private static Bitmap rotateImageIfRequired(Context context, Bitmap img, Uri selectedImage) throws IOException {

        InputStream input = context.getContentResolver().openInputStream(selectedImage);
        ExifInterface ei;
        if (Build.VERSION.SDK_INT > 23)
            ei = new ExifInterface(input);
        else
            ei = new ExifInterface(selectedImage.getPath());

        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return rotateImage(img, 90);
            case ExifInterface.ORIENTATION_ROTATE_180:
                return rotateImage(img, 180);
            case ExifInterface.ORIENTATION_ROTATE_270:
                return rotateImage(img, 270);
            default:
                return img;
        }
    }
    private static Bitmap rotateImage(Bitmap img, int degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
        img.recycle();
        return rotatedImg;
    }

你只需要调用handleSamplingAndRotationBitmap方法,你会得到一个可以自己设置大小的Bitmap。 PS: 案例 sumsung 手机旋转拍摄的一些图片是不正确的,所以我们也需要处理图片的方向,希望可以帮助你。

【讨论】:

    【解决方案2】:

    您可以简单地获取文件的大小。您需要在拍摄时存储图像。之后你可以使用下面的代码段来获取大小

    String imagePath = Environment.getExternalStorageDirectory() + "/yourImagefile.png";
    File imageFile = new File(imagePath );
    long filelength = imageFile .length();
    length = filelength/1024;
    

    length 以 KB 为单位提供大小。那么您可以添加如下 if 条件

     if(length>sizeyouwant){
        //delete image and toast message with info
        if(imageFile.exists()) {
           imageFile.delete();
        }
        Toast.makeText(getApplicationContext(),
        "Image is not saved due to image size exceeds limit....",
        Toast.LENGTH_SHORT).show();
      }
    

    【讨论】:

    • 这将检查文件大小,但我想在拍摄照片时限制 jpg 文件的大小。如果像 oneplus 或 samsung note 这样的相机,图像大小为 4-5 MB,那么用户将继续使用我的相机拍照,并且不接受超大图像,这将是无限循环。
    • 我不知道捕获时的检查大小。我认为这很难。但是您可以在捕获后减小尺寸,或者您可以为相机设置较低的分辨率,但它不能保证您的文件不超过一定的限制。
    • 拍照后压缩位图并再次保存。
    • stackoverflow.com/questions/37299490/… 如果你想要这个链接将帮助你在拍摄@madsDevTeam 后了解调整图像大小
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多