使用Android摄像头API,抢购的照片的方向始终是未定义 [英] Using Android Camera API, snapped photo's orientation is always Undefined

查看:158
本文介绍了使用Android摄像头API,抢购的照片的方向始终是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的照相机API,并采取总是由90度旋转的照片,我想旋转。

I use the camera api and the photo taken is always rotated by 90 degree, and i would like to rotate it.

所以首先我想知道图片的方向,这一点IM卡。 我总是得到UNDEFINDED取向两种方式。

So first of all i would like to know the picture's orientation and this point im stuck. I always getting UNDEFINDED orientation in both ways.

下面是code:

    @Override
        public void onPictureTaken(byte[] data, Camera camera) {


            //Bitmap Options for lowering quality the bitmap to save memory
            Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            options.inPreferredConfig = Bitmap.Config.RGB_565;

            //Make the bitmap
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

            //Making the path, the root will be fine for tests
            String path = Environment.getExternalStorageDirectory().toString();

            //output stream
            OutputStream outputStream = null;
            //Making the file as a jpg
            File file = new File(path, "tmp_pic" + ".jpg"); // the File to save to
            try {

                //Writing the file
                outputStream = new FileOutputStream(file);
                outputStream.flush();
                outputStream.close(); // do not forget to close the stream


                //Getting the orientation in both possible ways
                int ori = getOrientationFromExif(file.getPath());
                int ori2 = getOrientationFromUri(Uri.fromFile(file));
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            if (bitmap == null) {
                Toast.makeText(getApplicationContext(), "Error: Cant make photo.", Toast.LENGTH_SHORT).show();
            }
            else {
                PhotoTapView.photoViews.get(index).setPhotoImage(bitmap);
                finish();
            }
            cameraObject.release();
        }

为导向的功能:

The functions for orientation:

    //Getting orientation from file URI
    private int getOrientationFromUri(Uri imageUri) {
    String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION };
    Cursor cur = getContentResolver().query(imageUri, orientationColumn, null, null, null);
    int orientation = -1;
    if (cur != null && cur.moveToFirst()) {
        orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
    }
    Log.i("Orientation from Uri", orientation + "");
    return orientation;
}

    //Getting orientation from ExifInterface  
    private static int getOrientationFromExif(String imagePath) {
    int orientation = -1;
    try {
        ExifInterface exif = new ExifInterface(imagePath);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        Log.i("Orientation from Exif: ", exifOrientation + "");
        switch (exifOrientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                orientation = 270;
                Log.i("Orientation from Exif", "270");
            break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                orientation = 180;
                Log.i("Orientation from Exif", "180");
            break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                Log.i("Orientation from Exif", "90");
                orientation = 90;
            break;
            case ExifInterface.ORIENTATION_NORMAL:
                orientation = 0;
                Log.i("Orientation from Exif", "0 - Normal");
            break;
            case ExifInterface.ORIENTATION_UNDEFINED:
                orientation = -1;
                Log.e("Orientation from Exif", "UNDEFINED");
        }
    }
    catch (IOException e) {}
    return orientation;
}

日志输出:

01-14 19:46:09.468: E/Orientation from Exif(12411): UNDEFINED
01-14 19:46:09.468: I/Orientation from Uri(12411): -1

可能是什么问题?

What could be the problem?

推荐答案

我一直在解码/观察的Andr​​oid JPEG图像自2011年初以来,因为我发表了图像浏览应用程序。在早天,图像分别连接$ C $光盘中的传感器的本机取向和装置的实际取向被写入EXIF元数据。出发大约2年前,我注意到取向不再被写入到EXIF数据,而是,摄像头应用程序是旋转图像的像素编码JPEG文件之前。我的猜测是,这发生了,因为一些照片查看器(*咳嗽*的Windows *咳嗽*)忽略EXIF方向显示JPEG文件时,而是等待微软修复它,并指责安卓的做错事,他们决定利用更快的CPU /内存,只是旋转图像。

I have been decoding/observing Android JPEG images since early 2011 because I published an image viewing application. In the 'early' days, the images were encoded in the sensor's native orientation and the actual orientation of the device was written into the EXIF metadata. Starting about 2 years ago, I noticed that the orientation was no longer being written into the EXIF data and instead, the camera app was rotating the image pixels before encoding the JPEG files. My guess is that this occurred because some photo viewers (* cough * Windows * cough *) ignore the EXIF orientation when displaying JPEG files and instead of waiting for Microsoft to fix it and blaming Android for doing something wrong, they decided to make use of the faster CPU/memory and just rotate the image.

其结果是,不知道EXIF方向,只能确定一个照片被捕获在横向或纵向。的信息,该位只是一种假设,因为大多数相机传感器的宽度比他们高。

The result is that without knowing the EXIF orientation, one can only determine that a photo was captured in landscape or portrait orientation. This bit of info is only an assumption because the majority of camera sensors are wider than they are tall.

这篇关于使用Android摄像头API,抢购的照片的方向始终是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆