Android的形象定位问题,自定义相机活动自定义、形象、相机、问题

2023-09-04 03:59:59 作者:煮酒燃烟

我写了一个自定义的摄像头活动来处理一些问题,要求意向的图像捕捉的时候我一直有一定的Andr​​oid设备。用户能够任意选择保存图像或只使用返回的数据回从 OnPictureTakenCallback

我遇到的问题是相对于它被采取了正确的方向显示图像。我强迫该活动通过调用显示在纵向 SetRequestedOrientation

我怎么知道正确的方向的摄像头在当用户把画? 即用户可以拍摄照片以90(纵向)的旋转。

我试着去使用 getRotation()窗口管理器的默认显示器上,但设置要求的方向为纵向,只有返回 Surface.ROTATION_0

更新: 为了澄清我的其他问题,我怎么能确定从图片回调只是字节[] 数据如果用户是不保存图像的方向?

更新:在尝试下面这个code所有我得到是ExifInterface.ORIENTATION_NORMAL的答案。我也改变了我的code,以只保存在相机的文件返回我不知道有一个简单的方法来确定只具有字节[] 数据。

 私人PictureCallback mPicture =新PictureCallback()
    {
        @覆盖
        公共无效onPictureTaken(byte []的数据,摄像头摄像头)
        {
            文件目录=新File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES),
                    MyApp的);
            如果(!directory.exists())
            {
                如果(!directory.mkdirs())
                {
                    Log.d(照相机,无法创建目录来保存照片。);
                    返回;
                }
            }
            档案文件=新的文件(directory.getPath()+文件分割符+IMG_+ SimpleDateFormat.getDateTimeInstance()的toString()+.JPG。);
            FileOutputStream中FOS =新的FileOutputStream(文件);
            fos.write(数据);
            fos.close();
            ExifInterface EXIF​​ =新ExifInterface(file.getCanonicalPath());
            INT方向= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
            INT旋转= 0;

            开关(方向)
            {
                案例ExifInterface.ORIENTATION_ROTATE_90:
                   旋转= 90;
                   打破;
                案例ExifInterface.ORIENTATION_ROTATE_180:
                   旋转= 180;
                   打破;
                案例ExifInterface.ORIENTATION_ROTATE_270:
                   旋转= 270;
                   打破;
                默认情况下:
                   打破;
             }
        }
    };
 

解决方案

所以,你正面临着​​相机的方位一些问题。

此链接显示一个简单的摄像头采集活动的一个示例应用程序: http://labs.makemachine.net/2010/03/simple-android-photo-capture/

也许你应该尝试做这样的事情固定方向:

  ExifInterface EXIF​​ =新ExifInterface(_path);
          INT exifOrientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION,
          ExifInterface.ORIENTATION_NORMAL);

          INT旋转= 0;

          开关(exifOrientation){
          案例ExifInterface.ORIENTATION_ROTATE_90:
          旋转= 90;
          打破;

         案例ExifInterface.ORIENTATION_ROTATE_180:
         旋转= 180;
         打破;

         案例ExifInterface.ORIENTATION_ROTATE_270:
         旋转= 270;
         打破;
         }

           如果(旋转!= 0){
          INT W = bitmap.getWidth();
          INT H = bitmap.getHeight();

//设置pre旋转
          矩阵MTX =新的Matrix();
          。MTX preRotate(旋转);

         //旋转位图和放大器;转换为ARGB_8888,由苔丝要求
         位图= Bitmap.createBitmap(位图,0,0,W,H,MTX,假);
         位= bitmap.copy(Bitmap.Config.ARGB_8888,真正的);
       }
 
产品资讯 天极新闻频道 IT行业资讯 互联网资讯 电商资讯 打造科技行业权威资讯,坐看渠道风云变迁

I wrote a custom camera activity to handle some issues I've been having with certain android devices when calling intent image capture. The user is able to either select save image or just use the data returned back from the OnPictureTakenCallback.

The problem I'm having is displaying the image correctly with respect to the orientation it was taken. I force the activity to be displayed in portrait by calling SetRequestedOrientation.

How would I know the correct Orientation the camera was in when the user took the picture? i.e. The user could take the picture at a rotation of 90 (portrait).

I've tried to get to use the getRotation() on the window manager's default display, but with setting the requested orientation to portrait that only returns Surface.ROTATION_0.

Update: To clarify my other issue, how could I determine the orientation from just the byte[] data in the picture callback if the user were to not save the image?

Update: After trying the answers below with this code all I'm getting is ExifInterface.ORIENTATION_NORMAL. I've also changed my code to just save the file returned from the camera as I'm not sure there is an easy way to determine the orientation with just having the byte[] data.

    private PictureCallback mPicture = new PictureCallback() 
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) 
        {
            File directory = new File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES),
                    "MyApp");
            if(!directory.exists())
            {
                if(!directory.mkdirs())
                {
                    Log.d("CAMERA", "Unable to create directory to save photos.");
                    return;
                }
            }
            File file = new File(directory.getPath() + file.separator + "IMG_" + SimpleDateFormat.getDateTimeInstance().toString() + ".jpg");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data);
            fos.close();
            ExifInterface exif = new ExifInterface(file.getCanonicalPath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int rotate = 0;

            switch (orientation) 
            {
                case ExifInterface.ORIENTATION_ROTATE_90:
                   rotate = 90;
                   break; 
                case ExifInterface.ORIENTATION_ROTATE_180:
                   rotate = 180;
                   break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                   rotate = 270;
                   break;
                case default:
                   break;
             }
        }
    };

解决方案

SO you are facing some issue with the orientation of the camera.

This link shows an example app of a simple camera capture activity : http://labs.makemachine.net/2010/03/simple-android-photo-capture/

Maybe you should try fixing the orientation by doing something like this :

          ExifInterface exif = new ExifInterface(_path);
          int exifOrientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION,
          ExifInterface.ORIENTATION_NORMAL);

          int rotate = 0;

          switch (exifOrientation) {
          case ExifInterface.ORIENTATION_ROTATE_90:
          rotate = 90;
          break; 

         case ExifInterface.ORIENTATION_ROTATE_180:
         rotate = 180;
         break;

         case ExifInterface.ORIENTATION_ROTATE_270:
         rotate = 270;
         break;
         }

           if (rotate != 0) {
          int w = bitmap.getWidth();
          int h = bitmap.getHeight();

// Setting pre rotate
          Matrix mtx = new Matrix();
          mtx.preRotate(rotate);

         // Rotating Bitmap & convert to ARGB_8888, required by tess
         bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
         bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
       }

 
精彩推荐