Nar*_*ngh 21 android image filesize android-camera-intent onactivityresult
我的要求是将摄像头捕获的图像上传到服务器,但它应该小于500 KB.如果它大于500 KB,则需要将其减小到小于500 KB的大小(但稍微接近它)
为此,我使用以下代码 -
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
和
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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要,旋转图像.
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;
}
Run Code Online (Sandbox Code Playgroud)
这是最初和在减少文件大小的所有操作之后的图像文件大小的输出.
img_file_size:以KB为单位的文件大小(最初):3294
img_file_size:图像处理后的文件大小(KB):235
请参阅上面的差异(在输出中).没有那些操作的初始文件大小,以及那些压缩和其他操作之后.我需要这个尺寸有点接近500 kb.
上面的代码对我来说工作得有点好,因为它减少了图像文件的大小,使其小于500 KB.
但是,以下是上述代码的问题 -
此代码即使小于500 KB也会减小文件大小
如果它超过500 KB,减小的文件大小从500 KB变得太小,虽然我需要它更接近.
我需要摆脱2个问题.所以,需要知道我应该在上面的代码中操作什么.
因为我还想纠正EXIF方向(旋转图像),以及我上面提到的要求.
您可以在调整大小之前检查大小。如果位图的大小大于 500kb,则调整其大小。
同样为了使更大的位图更接近 500kb 大小,请检查大小之间的差异并相应地压缩。
if(sizeOf(reduced_bitmap) > (500 * 1024)) {
return getResizedBitmap(reduced_bitmap, maxSize, sizeOf(reduced_bitmap));
} else {
return reduced_bitmap;
}
Run Code Online (Sandbox Code Playgroud)
并在 resize Bitmap 计算大小差异并相应地压缩
| 归档时间: |
|
| 查看次数: |
9897 次 |
| 最近记录: |