Android将图像保存到Gallery中不是面向正确的



Android将图像保存到Gallery中的方向不正确intgetOrientationFromExif()之后获得的值始终相同:1。。。不知道如何解决。。。。请帮帮我!

你能帮我解决吗?内部出现问题

private PictureCallback mPicture = new PictureCallback()
{
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public void onPictureTaken(byte[] data, Camera camera){
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);    
try
{
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close(); 
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
Bitmap myBitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
myBitmap = rotateImage(getOrientationFromExif(pictureFile), myBitmap); 
myBitmap.compress(Bitmap.CompressFormat.JPEG, 70 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
fos = new FileOutputStream(pictureFile);
fos.write(bitmapdata);
fos.close(); 
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
Uri.parse("file://"+ mediaStorageDir)));
}
catch(FileNotFoundException e)
{
Log.d(TAG, "File not found: "+e.getMessage());
}
catch(IOException e)
{
Log.d(TAG, "Error accessing file: "+e.getMessage());
}
}
};
public Bitmap rotateImage(int angle, Bitmap bitmapSrc) 
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmapSrc, 0, 0, 
bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}
public int getOrientationFromExif(File imagePath) 
{
int orientation = -1;
try 
{   
ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
ExifInterface.ORIENTATION_NORMAL);
System.out.println("yuri"+exifOrientation);
switch (exifOrientation) 
{
case ExifInterface.ORIENTATION_ROTATE_270:      
orientation = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
orientation = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;
break;
default:
break;
}
} catch (IOException e) {
Log.e(TAG, "Unable to get image exif orientation", e);
}
return orientation;
}
public static boolean isSdPresent() {   
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

private static File getOutputMediaFile(int type){
File mediaFile = null; 
if(isSdPresent() == false)
{
Log.d(TAG, "There is no Sd card. Cannot use the camera");
}
else
{
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),".");
if(!mediaStorageDir.exists())
{
if(!mediaStorageDir.mkdirs())
{
Log.d("WorldCupApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());       
if (type == MEDIA_TYPE_IMAGE)
{        
mediaFile = new File(mediaStorageDir.getPath() + "IMG_"+ timeStamp + ".JPEG");    
} 
else 
{       
return null;  
}          
}
return mediaFile;
}       
ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
ExifInterface.ORIENTATION_NORMAL);

这里的问题总是1方向,这意味着它不需要旋转:(

当您为自定义相机实现将活动固定为横向模式时,EXIF数据将始终具有TAG_ORIENTATION的ORIENTATION_NORMAL。这与activity的副作用相同。getWindowManager().getDefaultDisplay().getration()总是返回ROTATION_90。

我通过OrientationEventListener检测设备旋转,然后将onPictureTaken回调中的EXIF方向标记重写为旋转的正确值,从而解决了这个问题。

不确定这种行为是否与设备有关,这是运行4.1.2的Galaxy S2上的行为。

最新更新