Tôi đang cố gắng gửi hình ảnh đến máy chủ. Trước khi gửi nó, tôi đang giảm kích thước và chất lượng của nó, và sau đó sửa chữa bất kỳ vấn đề xoay. Vấn đề của tôi là, sau khi xoay hình ảnh, khi tôi lưu nó, tập tin lớn hơn trước. Trước khi kích thước xoay là 10092 và sau khi xoay là 54226Bitmap được nén với chất lượng = 100 kích thước tệp lớn hơn bản gốc
// Scale image to reduce it
Bitmap reducedImage = reduceImage(tempPhotoPath);
// Decrease photo quality
FileOutputStream fos = new FileOutputStream(tempPhotoFile);
reducedImage.compress(CompressFormat.JPEG, 55, fos);
fos.flush();
fos.close();
// Check and fix rotation issues
Bitmap fixed = fixRotation(tempPhotoPath);
if(fixed!=null)
{
FileOutputStream fos2 = new FileOutputStream(tempPhotoFile);
fixed.compress(CompressFormat.JPEG, 100, fos2);
fos2.flush();
fos2.close();
}
public Bitmap reduceImage(String originalPath)
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inPurgeable = true;
o.inInputShareable = true;
BitmapFactory.decodeFile(originalPath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 320;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inPurgeable = true;
o2.inInputShareable = true;
o2.inSampleSize = scale;
Bitmap bitmapScaled = null;
bitmapScaled = BitmapFactory.decodeFile(originalPath, o2);
return bitmapScaled;
}
public Bitmap fixRotation(String path)
{
Bitmap b = null;
try
{
//Find if the picture is rotated
ExifInterface exif = new ExifInterface(path);
int degrees = 0;
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6"))
degrees = 90;
else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8"))
degrees = 270;
else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3"))
degrees = 180;
if(degrees > 0)
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inPurgeable = true;
o.inInputShareable = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, o);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(degrees);
b = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
}
catch(Exception e){e.printStackTrace();}
return b;
}
Cảm ơn Geobits, nhưng tôi đang sử dụng chất lượng 100 với tệp được tạo trước đó (chất lượng 55), không phải với tệp gốc. Hoặc đó là những gì tôi nghĩ rằng tôi đang làm. Sau khi lưu hình ảnh với chất lượng 55, kích thước tệp là 10092. Sau đó, tôi lấy tệp đó, xoay tệp và lưu tệp với chất lượng 100 và kích thước tệp bây giờ là 54226 –
Cảm ơn Geobits, tôi nghĩ rằng tôi hiểu nó now.Is có cách nào để tôi có thể sửa chữa các vấn đề xoay, giảm kích thước và chất lượng của nó, mà không có một tập tin cuối cùng lớn hơn bản gốc? Tôi đã thử quay đầu tiên và sau đó giảm kích thước và chất lượng, nhưng nó đã cho tôi OutOfMemoryException khi quay. Ý tưởng nào? –
Đã thêm vào câu trả lời – Geobits