我的应用程序拍照然后上传它.由于拍摄的照片尺寸很大,我必须缩小尺寸然后再上传.因此,我使用以下代码来减少存储在SD卡中某处的相机所拍摄图像的大小.但是我不知道代码有什么问题,如果是肖像,图像会逆时针旋转90度,否则在横向模式拍摄时图像会很好.
以下是我用来减小大小的代码.
public File getReducedImage(File mediaFile){
Bitmap b = decodeFile(mediaFile);
return getfileFromBitmap(b, mediaFile.getPath());
}
private File getfileFromBitmap(Bitmap b, String path) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(path);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
Log.v(TAG, "returned");
return f;
} catch (IOException e) {
// Todo Auto-generated catch block
e.printstacktrace();
Log.v(TAG, "Exception caught");
return null;
}
//write the bytes in file
}
private Bitmap decodeFile(File f){
final int IMAGE_MAX_SIZE=400;
Bitmap b = null;
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE /
(double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
Log.v(TAG, "error in bitmap conversion");
e.printstacktrace();
}
return b;
}
编辑:
实际上问题是图像一旦调整大小就丢失了方向信息.我通过适当的旋转解决了它.以下是我的解决方案.希望它会帮助某人.
public File getReducedImage(File mediaFile){
Bitmap b = decodeFileWithRotationIfNecessary(mediaFile);
File f = getfileFromBitmap(b, mediaFile.getPath());
return f;
}
private File getfileFromBitmap(Bitmap b, String path) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(path);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
return f;
} catch (IOException e) {
// Todo Auto-generated catch block
e.printstacktrace();
Log.v(TAG, "Exception caught");
return null;
}
// write the bytes in file
}
private Bitmap decodeFileWithRotationIfNecessary(File f) {
final int IMAGE_MAX_SIZE = 400;
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(IMAGE_MAX_SIZE
/ (double) Math.max(o.outHeight, o.outWidth))
/ Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
Log.v(TAG, "error in bitmap conversion");
e.printstacktrace();
}
Bitmap bMapRotate = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getHeight(), getMatrix(f), true);
return bMapRotate;
}
private Matrix getMatrix(File f) {
Matrix mat = new Matrix();
mat.postRotate(90);
try {
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
switch (orientation) {
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
Log.v(TAG, "flip horizontal");
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
Log.v(TAG, "flip vertical");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Log.v(TAG, "rotate 180");
mat.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Log.v(TAG, "rotate 90");
break;
case ExifInterface.ORIENTATION_ROTATE_270:
Log.v(TAG, "rotate 270");
mat.postRotate(180);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
Log.v(TAG, "transpose");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
Log.v(TAG, "undefined");
break;
case ExifInterface.ORIENTATION_norMAL:
Log.v(TAG, "normal");
mat.postRotate(270);
break;
default:
Log.v(TAG, "default");
// mat.postRotate(0);
break;
}
} catch (IOException e) {
// Todo Auto-generated catch block
e.printstacktrace();
Log.v(TAG, "error in finding exif information");
}
return mat;
}
解决方法:
一个原因可能是您没有保留EXIF数据:
ExifInterface ei = new ExifInterface(<file-path>);
int o = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_norMAL);
//--decode -- encode save--
ExifInterface ei2 = new ExifInterface(<new-file-path>);
ei2.setAttribute(ExifInterface.TAG_ORIENTATION,o);
ei2.saveAttributes();