Android 在“摄影机意图”中选择图像大小

Android 在“摄影机意图”中选择图像大小,android,android-studio,android-camera-intent,Android,Android Studio,Android Camera Intent,我是新加入这个论坛的,所以请容忍我,如果有错误,请温和地指出, 所以我正在做一个项目,我正在上传图片到服务器,现在我想限制图片的大小,我给了一个选项“点击图片”,我的代码将打开默认的相机意图,点击图片,或者“从图库中选择”。 我的问题是关于“单击图像”,现在当用户单击图像时,我可以预设可以单击的图像最大大小吗?您可以简单地获得文件的大小。您需要在拍摄图像时存储图像。之后,你可以使用下面的代码段获得大小 String imagePath = Environment.getExternalStora

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

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

String imagePath = Environment.getExternalStorageDirectory() + "/yourImagefile.png";
File imageFile = new File(imagePath );
long filelength = imageFile .length();
length = filelength/1024;
这个
长度
以KB为单位显示大小。然后您可以添加如下条件

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

您可以在onActivityResult方法中调整图像大小,请尝试以下代码段

 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;
    }
公共静态位图句柄采样和旋转位图(上下文,Uri selectedImage)
抛出IOException{
int MAX_HEIGHT=1024;
int MAX_WIDTH=1024;
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
InputStream imageStream=context.getContentResolver().openInputStream(SelecteImage);
解码流(imageStream,null,选项);
imageStream.close();
options.inSampleSize=计算样本大小(选项、最大宽度、最大高度);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
imageStream=context.getContentResolver().openInputStream(SelecteImage);
位图img=BitmapFactory.decodeStream(imageStream,null,选项);
img=rotateImageIfRequired(上下文、img、selectedImage);
返回img;
}
私有静态int-calculateInSampleSizes(BitmapFactory.Options、,
输入要求宽度,输入要求高度){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
//计算高度和宽度与所需高度和宽度的比率
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
//选择最小比率作为inSampleSize值,这将保证最终图像
//两个尺寸都大于或等于要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
//这提供了一些额外的逻辑,以防图像有奇怪的颜色
//宽高比。例如,全景图可能具有更大的宽高比
//宽度大于高度。在这些情况下,总像素可能仍然
//结果是太大而不能舒服地放在记忆中,所以我们应该
//在图像下采样时更具攻击性(=采样尺寸更大)。
最终浮点总数像素=宽度*高度;
//任何超过所需像素2倍的像素,我们将进一步采样
最终浮点totalReqPixelsCap=reqWidth*reqHeight*2;
而(totalPixels/(inSampleSize*inSampleSize)>totalReqPixelsCap){
inSampleSize++;
}
}
返回样本大小;
}
私有静态位图rotateImageIfRequired(上下文上下文、位图img、Uri selectedImage)引发IOException{
InputStream输入=context.getContentResolver().openInputStream(SelecteImage);
出口接口ei;
如果(Build.VERSION.SDK_INT>23)
ei=新的ExiFinInterface(输入);
其他的
ei=新的ExiFinInterface(selectedImage.getPath());
int-orientation=ei.getAttributeInt(ExifInterface.TAG\u-orientation,ExifInterface.orientation\u-NORMAL);
开关(方向){
外壳出口接口。方向旋转90:
返回旋转图像(img,90);
外壳出口接口.方向旋转180:
返回旋转图像(img,180);
外壳出口接口方向旋转270:
返回旋转图像(img,270);
违约:
返回img;
}
}
专用静态位图旋转图像(位图img,int度){
矩阵=新矩阵();
矩阵。旋转后(度);
Bitmap rotatedImg=Bitmap.createBitmap(img,0,0,img.getWidth(),img.getHeight(),matrix,true);
img.recycle();
返回旋转dIMG;
}
您只需要调用handleSamplingAndRotationBitmap方法,就可以得到一个可以自己设置大小的位图。
PS:如果sumsung手机的旋转拍摄到的一些图片不正确,那么我们也需要处理图片的方向,希望能对您有所帮助

您好,您所说的大小、分辨率或以MB为单位的实际大小是什么意思?大小以MB为单位,但我想两者都是相互依赖的,降低分辨率,降低大小这将检查文件大小,但我想在拍摄照片时限制jpg文件的大小。如果像oneplus或samsung note这样的相机,图像大小为4-5 MB,那么用户将继续使用我的相机拍照,超大图像将不会被接受,这将是无限循环。我不知道在拍摄时检查大小。我认为这很难。但是你可以在拍摄后缩小尺寸,或者你可以为相机设置一个较低的分辨率,但它不能保证你的文件不会超过一定的限制。拍摄完图像后,压缩位图并再次保存。如果您需要,此链接将帮助您了解在拍摄@madsDevTeam后调整图像大小的方法