安卓相机:在所有设备上获得直接照片

问题描述:

所以我得到了这个简单的应用程序,打开相机(正面或背面),拍照,张贴它。安卓相机:在所有设备上获得直接照片

尽管显示方向,我仍设法得到直观的预览。

问题是当我得到图片。例如,如果我使用纵向模式拍摄具有不同设备的照片,则拍摄的图片将根据使用的设备不同角度进行旋转。

我所要求很简单:你知不知道我怎样才能直图片(纵向或横向,根据旋转),并确保它工作在每个设备上?

好”如果你们有一个解决方案,请与大家共享,我永远感激!

+0

我想用户正在拍照并且您没有检测到设备方向。我不编码android,但你必须做的是:检测设备的方向。解析收到的数据并旋转与方向对应的图像 – wonderwhy 2014-09-05 16:16:58

默认情况下,不同的设备在其图片上使用不同的方向。这可能很烦人。你需要做的是找到这个方向,并相应地翻转图像。

下面是一些代码,我在我的应用程序中使用: 注:我敢肯定,这是不完美的。如果任何人有关于如何改善这一点的建议,请随时评论/编辑!

private int getOrientation(String pathToYourImage) throws IOException{ 
    ExifInterface exif = new ExifInterface(path); 
    int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

    if (rotation == ExifInterface.ORIENTATION_ROTATE_90){ 
     return 90; 
    } else if (rotation == ExifInterface.ORIENTATION_ROTATE_180){ 
     return 180; 
    } else if (rotation == ExifInterface.ORIENTATION_ROTATE_270){ 
     return 270; 
    } 
    return 0;  
} 

事遂所愿

if (orientation > 0){ 
    originalImage = rotateBitmap(originalImage, orientation); 
} 

private Bitmap rotateBitmap(Bitmap srcBitmap, int angle){ 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    Bitmap rotatedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true); 
    srcBitmap.recycle(); 
    return rotatedBitmap; 
} 

这可能无法完全满足您的需求,但它运作良好,对我来说足够。询问你是否还有其他问题!