私のアプリケーションは写真を撮ってアップロードします。撮影した写真はサイズが大きいので、サイズを縮小してアップロードする必要があります。そのため、次のコードを使用して、カメラで撮影した画像のサイズを縮小し、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(ファイル 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;
}