简体   繁体   English

Android相机:在所有设备上获得清晰的照片

[英]Android camera: get a straight picture on all devices

So i got this simple app, open the camera (either front or back), take a picture, post it. 所以我得到了这个简单的应用程序,打开相机(正面或背面),拍照,发布。

I managed to get a straight preview, despite the display orientation. 无论显示方向如何,我都设法获得了直接的预览。

The problem is when I get the picture. 问题是当我得到照片时。 If for example I take a photo with different devices in portrait mode, the resulting pictures are rotated of different angles, depending which device was used. 例如,如果我以纵向模式使用不同的设备拍摄照片,则生成的图片将旋转不同的角度,具体取决于所使用的设备。

All I'm asking is simple: do you know how I can get straight pictures (either portrait or landscape, according to the rotation) and make sure it works on every device? 我要问的很简单:您知道我如何获取直线图片(根据旋转情况是纵向还是横向),并确保它可以在所有设备上正常工作?

Well' if any of you has a solution,please share it,I'll be eternally grateful! 好吧,如果您有任何解决方案,请与我们分享,我将永远感激不已!

Different devices use different orientations on their pictures by default. 默认情况下,不同的设备在其图片上使用不同的方向。 This can be annoying. 这可能很烦人。 What you need to do is find that orientation, and flip the image correspondingly. 您需要做的就是找到该方向,然后相应地翻转图像。

Here's some code that I use in my application: Note: I'm sure this isn't perfect. 这是我在应用程序中使用的一些代码:注意:我确定这不是完美的。 If anyone has suggestions on how to improve this, please feel free to comment/edit! 如果有人对如何改善此问题有任何建议,请随时发表评论/编辑!

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;       
}

then something like 然后像

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;
}

This might not completely suit your needs, but it works well enough for me. 这可能无法完全满足您的需求,但是对我来说效果很好。 Ask if you have any more questions! 询问您还有其他问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM