从Android服务器下载时,图像倾斜90度?

Sam*_*ens 5 android imagedownload

处理我们捕获图像并通过服务器上传的应用程序.应用程序在 Android和I Phone中.当我们从Android发布图像时,它们的数量是Kilo Bytes但是当我们从I Phone发布图像时它们是MB大小.

当我们在IPHONE 5上使用URL在浏览器上发布图像时,它们看起来很好,但是当我们在Android设备中下载该图像并在图像视图中显示时,它们会向左侧倾斜90度.

在Android或I Phone中下载图像后,我没有使用任何旋转代码.

在I Phone中,图像看起来很好.

从Android捕获的图像也是可见的.从I Phone中捕获的低分辨率图像也可以直接在Android中看到.

从Android上传的图片:

https://s3.amazonaws.com/WeddingApp/Weddingimage/933_6_stan.jpg
Run Code Online (Sandbox Code Playgroud)

从I Phone上传的图片:

https://s3.amazonaws.com/WeddingApp/Weddingimage/937_6_stan.jpg


public static boolean downloadFile(final String fileURL,File directory,Context CONTEXT){
        try{
            URL url = new URL(fileURL);

            URLConnection ucon = url.openConnection();
            ucon.setReadTimeout(35000);
            ucon.setConnectTimeout(10000);

            InputStream is = ucon.getInputStream();
            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);

            File file = directory;

            if (file.exists())
            {
                file.delete();
            }
            file.createNewFile();

            FileOutputStream outStream = new FileOutputStream(file);
            byte[] buff = new byte[5 * 1024];

            int len;
            while ((len = inStream.read(buff)) != -1)
            {
                outStream.write(buff, 0, len);
            }

            outStream.flush();
            outStream.close();
            inStream.close();

        }
        catch (IOException e){  //IF SDCARD NOT EXIST THEN PASS RESPONSE TRUE`  
            e.printStackTrace();
            return false;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    } 
Run Code Online (Sandbox Code Playgroud)

请建议我.

san*_* mk 5

这是因为iPhone傻乎乎的新相机实现.我在下面找到了研究(下面给出的来源):

1. iPhone相机对"向上"方向的解释是从我们所知的实际"向上"方向旋转90度.你和我称之为"up",是iphone的"左"或"右".相机的方向已更改.这样做是为了让他们可以支持使用音量按钮拍照.

2.但是,要注意其他设备和相机具有正常方向,并且这些设备和浏览器正确显示图像,iOS会将EXIF数据添加到上传的图像中.这是在图片上传时添加的.如链接中所述,EXIF是一个元数据,其中包含图像实际应该如何显示的所有信息.

3.下载图像的目标平台应该读取EXIF数据,然后显示图像的正确表示.因此,如果目标平台读取EXIF,它将意识到所接收的图像是90度旋转,并且它应该依赖于EXIF数据,而不是接收的是什么.这就是iPhone相机应用程序可以正确显示图像的原因.

4然而,并非所有人都阅读EXIF.知道EXIF的应用和浏览器,读取图像的EXIF数据并正确显示.但是那些不了解EXIF的人,不会读取那些数据,并且显示的图像与接收完全一致 - > 旋转90度

5人们已经解决了这个问题,在拍摄照片之前将iPhone旋转90度(以便在拍摄照片之前将相机朝向正确)

资源:

1. 来源1

2. 来源2

其他有同样问题的人:

1. SO Post 1

2. SO Post 2


Jun*_*dge 2

该问题似乎与图像上的EXIF数据有关。我们必须在显示图像之前对其进行处理。似乎并不是每个相机都会输出 EXIF 数据,因为这个问题只发生在我的某些 Android 手机上。看看:http://commons.wikimedia.org/wiki/Commons: Exif#Orientation_.28rotation_and_mirroring.29

编辑: 我们可以实现类似的东西:

public Bitmap getBitmap(String path){
    Bitmap tmpBitmap = BitmapFactory.decodeFile(path);
            Bitmap bitmap = null;
            if(path != null){
                try {
                    ExifInterface ei = new ExifInterface(path);
                    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                    Matrix mtx = new Matrix();
                    int w = tmpBitmap.getWidth();
                    int h = tmpBitmap.getHeight();
                    switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        //rotate CCW
                        mtx.preRotate(-90);
                        bitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, w, h, mtx, true);
                        break;

                    case ExifInterface.ORIENTATION_ROTATE_270:
                        //rotate CW
                        mtx.preRotate(90);
                        bitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, w, h, mtx, true);
                        break;

                        //CONSIDER OTHER CASES HERE....

                    default:
                        bitmap = tmpBitmap;
                        break;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
    return bitmap;
}
Run Code Online (Sandbox Code Playgroud)

问候。