android减少摄像头捕获图像的文件大小小于500 kb [英] android reduce file size for camera captured image to be less than 500 kb

查看:227
本文介绍了android减少摄像头捕获图像的文件大小小于500 kb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是将摄像头捕获的图像上传到服务器,但它应该小于500 KB。如果它大于500 KB,则需要将其缩小到小于500 KB (但稍微靠近它)

My requirement is to upload camera captured image to the server, but it should be less than 500 KB. In case, if it is greater than 500 KB, it needs to be reduced to the size less than 500 KB (but somewhat closer to it)

为此,我使用以下代码 -

For this, I am using the following code -

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == getActivity().RESULT_OK) {

                    if (requestCode == REQUEST_CODE_CAMERA) {

                        try {

                            photo = MediaStore.Images.Media.getBitmap(
                                    ctx.getContentResolver(), capturedImageUri);
                            String selectedImagePath = getRealPathFromURI(capturedImageUri);

                            img_file = new File(selectedImagePath);

                            Log.d("img_file_size", "file size in KBs (initially): " + (img_file.length()/1000));

                            if(CommonUtilities.isImageFileSizeGreaterThan500KB(img_file)) {
                                photo = CommonUtilities.getResizedBitmapLessThan500KB(photo, 500);
                            }
                            photo = CommonUtilities.getCorrectBitmap(photo, selectedImagePath);


//  // CALL THIS METHOD TO GET THE URI FROM THE BITMAP

                            img_file = new File(ctx.getCacheDir(), "image.jpg");
                            img_file.createNewFile();

//Convert bitmap to byte array
                            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                            photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

//write the bytes in file
                            FileOutputStream fo = new FileOutputStream(img_file);
                            fo.write(bytes.toByteArray());

// remember close de FileOutput
                            fo.close();
                            Log.d("img_file_size", "file size in KBs after image manipulations: " + (img_file.length()/1000));


                        } catch (Exception e) {
                            Logs.setLogException(class_name, "onActivityResult(), when captured from camera", e);
                        }


                    } 

            }
        } catch (Exception e) {
            Logs.setLogException(class_name, "onActivityResult()", e);
        } catch (OutOfMemoryError e) {
            Logs.setLogError(class_name, "onActivityResult()", e);

        }
    }

public static Bitmap getResizedBitmapLessThan500KB(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();



        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 0) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        Bitmap reduced_bitmap = Bitmap.createScaledBitmap(image, width, height, true);
        if(sizeOf(reduced_bitmap) > (500 * 1000)) {
            return getResizedBitmap(reduced_bitmap, maxSize);
        } else {
            return reduced_bitmap;
        }
    }

如果需要,可以旋转图像。

To rotate the image, if needed.

public static Bitmap getCorrectBitmap(Bitmap bitmap, String filePath) {
        ExifInterface ei;
        Bitmap rotatedBitmap = bitmap;
        try {
            ei = new ExifInterface(filePath);

            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            Matrix matrix = new Matrix();
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    matrix.postRotate(90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    matrix.postRotate(180);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    matrix.postRotate(270);
                    break;
            }

            rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rotatedBitmap;
    }

这是最初的图像文件大小的输出,并且在所有操作之后减小文件大小。

Here is the output of the image file size initially and after all the operations to reduce file size.


img_file_size:文件大小(KBs)(最初):3294

img_file_size﹕ file size in KBs (initially): 3294

img_file_size:图像处理后的文件大小(KB):235

img_file_size﹕ file size in KBs after image manipulations: 235

查看上面的差异(在输出中)。没有那些操作的初始文件大小,以及那些压缩和其他操作之后。我需要这个大小接近500 kb。

上面的代码对我来说有点好,因为它将图像文件大小减小到使它低于500 KB。

The above code is working somewhat fine for me, as it is reducing the image file size to make it less than 500 KB.

但是,以下是上述代码的问题 -


  • 此代码正在减小文件大小,即使其小于500 KB

  • This code is reducing the file size even if its less than 500 KB

以防万一它超过500 KB,减小的文件大小从500 KB变得太小了,虽然我需要它更接近。

In case it is more than 500 KB, the reduced file size becomes too less from 500 KB, , though I need it somewhat closer.

我需要摆脱2个问题。所以,需要知道我应该在上面的代码中操作什么。

I need to get rid off above 2 issues. So, need to know what should I manipulate in the above code.

因为我还想纠正EXIF方向(旋转图像),以及我上面提到的要求。

As I also want to correct the EXIF-orientation (rotated images), along with my above mentioned requirement.

推荐答案

如果位图大小超过500kb,您可以在调整大小之前检查大小,然后调整大小。

You can check size before Resizing if the bitmap is larger than 500kb in size then resize it .

另外,为了使更大的位图更接近500kb大小,请相应地检查尺寸和压缩之间的差异。

Also for making larger bitmap nearer to 500kb size, check the difference between size and compress accordingly .

 if(sizeOf(reduced_bitmap) > (500 * 1024)) {
                    return getResizedBitmap(reduced_bitmap, maxSize, sizeOf(reduced_bitmap));
                } else {
                    return reduced_bitmap;
                }

并在调整大小时位图计算大小差异并相应地压缩

and in resize Bitmap calculate the difference in size and compress accordingly

这篇关于android减少摄像头捕获图像的文件大小小于500 kb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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