EXIF方向标签值始终为0图像拍摄的人像摄像头应用程序的Andr​​oid人像、应用程序、摄像头、图像

2023-09-12 04:07:39 作者:ヌ月夜し凉城マ

我在纵向模式下相机应用程序,它需要来自前端和后端cameras.I照片我保存的图像在我的SD卡,并设法找到相应的EXIF值给出0 always.But我收到的预期的exif取向值存储在设备(如下载的图片)的其他图像

我怎样才能解决这个问题?谁能帮助我?

下面是一个用来保存图片和寻找方向的code

  PictureCallback myPictureCallback_JPG =新PictureCallback(){

    @覆盖
    公共无效onPictureTaken(byte []的为arg0,摄像机ARG1){
        // TODO自动生成方法存根


                尝试 {
                    文件APP_FILE_PATH =新的文件(Environment.getExternalStorageDirectory()
                            .getPath()+/ MYAPP /);
                    如果(!APP_FILE_PATH.exists()){
                        APP_FILE_PATH.mkdirs();
                    }
                    档案文件=新的文件(APP_FILE_PATH,image.jpg文件);

                    FileOutputStream中FOS =新的FileOutputStream(文件);
                    fos.write(为arg0);
                    fos.close();
imageFileUri = Uri.fromfile(文件); getApplicationContext()。getContentResolver()。有NotifyChange(
                          imageFileUri,NULL);
                sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse(文件://
                                    + Environment.getExternalStorageDirectory())));
                    ExifInterface EXIF​​ =新ExifInterface(file.getAbsolutePath());
                    INT方向= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);



                }赶上(例外五){

                }



    }
};
 

以下是$ C $下创建surphace和改变功能

  @覆盖
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,
INT高度){
名单<大小>大小= parameters.getSupported previewSizes();
大小optimalSize = getOptimal previewSize(尺寸,宽,高);
parameters.set previewSize(optimalSize.width,optimalSize.height);

camera.setParameters(参数);
camera.start preVIEW();
 启动preVIEW();

}
 @覆盖
 公共无效surfaceCreated(SurfaceHolder持有者){


 尝试 {

     如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.GINGERBREAD){
          Camera.CameraInfo信息=新Camera.CameraInfo();

          的for(int i = 0; I< Camera.getNumberOfCameras();我++){
            Camera.getCameraInfo(ⅰ,信息);

            如果(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
              相机= Camera.open(ⅰ);
              defaultCameraId =我;
            }
          }
        }

        如果(相机== NULL){
          相机= Camera.open();
        }




        尝试 {
            camera.set previewDisplay(surfaceHolder);
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        Camera.Parameters参数= camera.getParameters();
        android.hardware.Camera.CameraInfo信息=
                新android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(defaultCameraId,资讯);
        INT旋转= this.getWindowManager()。getDefaultDisplay()
                .getRotation();
        如果(的Integer.parseInt(Build.VERSION.SDK)> = 8)
        {

             INT度= 0;
             开关(旋转){
                 案例Surface.ROTATION_0:
                     度= 0;打破;
                 案例Surface.ROTATION_90:
                     度= 90;打破;
                 案例Surface.ROTATION_180:
                     度= 180;打破;
                 案例Surface.ROTATION_270:
                     度= 270;打破;
             }
             INT结果;
             如果(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
                 结果=(info.orientation +度)%360;
                 结果=(360  - 结果)%360;
             }其他{//背向
                 结果=(info.orientation  - 度+ 360)%360;
             }
             camera.setDisplayOrientation(结果);

        }
        其他
        {
            parameters.set(方向,肖像);
        }


        camera.setParameters(参数);



}赶上(抛出:IllegalArgumentException E){
    // TODO自动生成的catch块
    e.printStackTrace();
}赶上(SecurityException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
}


}
 私人大小getOptimal previewSize(名单<大小>的大小,INT W,INT高){
            最终双ASPECT_TOLERANCE = 0.1;
            双targetRatio =(双)W / H;
            如果(大小== NULL)返回NULL;

            大小optimalSize = NULL;
            双minDiff = Double.MAX_VALUE;

            INT targetHeight = H;


            对于(尺寸大小:大小){
                双率=(双)size.width / size.height;
                如果(Math.abs(比 -  targetRatio)> ASPECT_TOLERANCE)继续;
                如果(Math.abs(size.height  -  targetHeight)< minDiff){
                    optimalSize =大小;
                    minDiff = Math.abs(size.height  -  targetHeight);
                }
            }


            如果(optimalSize == NULL){
                minDiff = Double.MAX_VALUE;
                对于(尺寸大小:大小){
                    如果(Math.abs(size.height  -  targetHeight)< minDiff){
                        optimalSize =大小;
                        minDiff = Math.abs(size.height  -  targetHeight);
                    }
                }
            }
            返回optimalSize;
        }
 

解决方案

我也是在三星设备面临着同样的问题,后来我实现的 ExifInterface 并成功地解决了。

在任何模式下图像将被枪杀它会一直保存在纵向模式下,虽然也取回来在纵向模式。的code下面我来实现我的目标,我在相机背面实施,不知道从相机。

相机意图@

 意向意图=新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);
            startActivityForResult(意向,1212);
 

onActivityResult @

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,因此code,数据);

        如果(要求code == 1212){
        sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ Environment.getExternalStorageDirectory())));
        点阵位图;
        //bitmap=GlobalMethods.de$c$cSampledBitmapFromResource(_path,80,80);
        位= GlobalMethods.de codeFILE(_path);
        如果(位图== NULL){
            imgMed.setImageBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.add_photo));
        }
        其他 {
            imgMed.setImageBitmap(位);
            imgMed.setScaleType(ScaleType.FIT_XY);

        }
    }
        }
 

德codeFILE @

 公共静态位图德codeFILE(字符串路径){

          INT方向;

             尝试 {

                 如果(路径== NULL){

                     返回null;
                 }
                 //德code图像尺寸
                 BitmapFactory.Options O =新BitmapFactory.Options();
                 o.inJustDe codeBounds = TRUE;

                 //找到正确的比例值。它应该是2的幂。
                 最终诠释REQUIRED_SIZE = 70;
                 INT width_tmp = o.outWidth,height_tmp = o.outHeight;
                 int标= 4;
                 而(真){
                     如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2'; REQUIRED_SIZE)
                         打破;
                     width_tmp / = 2;
                     height_tmp / = 2;
                     规模++;
                 }
                 //德code与inSampleSize
                 BitmapFactory.Options O2 =新BitmapFactory.Options();
                 o2.inSampleSize =规模;
                 位图BM = BitmapFactory.de codeFILE(路径,O2);


                 点阵位图= BM;

                 ExifInterface EXIF​​ =新ExifInterface(路径);
                 方向= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,1);
                 Log.e(方向,+方向);
                 矩阵M =新的Matrix();

                 如果((取向== 3)){

                 m.postRotate(180);
                 m.postScale((浮点)bm.getWidth(),(浮动)bm.getHeight());

//如果(米。preRotate(90)){
                 Log.e(定向,+方向);

                 位图= Bitmap.createBitmap(宽多重峰,0,0,bm.getWidth(),bm.getHeight()中,m,真);
                 返回的位图;
                 }
                 否则,如果(定向== 6){

                  m.postRotate(90);

                  Log.e(定向,+方向);

                  位图= Bitmap.createBitmap(宽多重峰,0,0,bm.getWidth(),bm.getHeight()中,m,真);
                     返回的位图;
                 }

                 否则,如果(定向== 8){

                  m.postRotate(270);

                  Log.e(定向,+方向);

                  位图= Bitmap.createBitmap(宽多重峰,0,0,bm.getWidth(),bm.getHeight()中,m,真);
                     返回的位图;
                 }
                 返回的位图;
             }
             赶上(例外五){
             }
             返回null;
         }
 

I have a camera app in portrait mode which takes pictures from both front and back end cameras.I am saving the image in my sd card and try to find the corresponding exif value which gives 0 always.But i am getting the the expected exif orientation value for the other images stored in the device(like downloaded pictures).

How can i fix this ? Can anyone help me out ?

Here is the code used to save the picture and the find the orientation

PictureCallback myPictureCallback_JPG = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub


                try {
                    File APP_FILE_PATH = new File(Environment.getExternalStorageDirectory()
                            .getPath() + "/Myapp/");
                    if (!APP_FILE_PATH.exists()) {
                        APP_FILE_PATH.mkdirs();
                    }
                    File file = new File(APP_FILE_PATH, "image.jpg");

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(arg0);
                    fos.close();
imageFileUri=Uri.fromfile(file);                           getApplicationContext().getContentResolver().notifyChange(
                          imageFileUri, null);
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse("file://"
                                    + Environment.getExternalStorageDirectory())));
                    ExifInterface exif = new ExifInterface(file.getAbsolutePath());
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);



                } catch (Exception e) {

                }



    }
};

Following is the code for surphace created and changed functions

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, width, height);
parameters.setPreviewSize(optimalSize.width, optimalSize.height);

camera.setParameters(parameters);
camera.startPreview();
 startPreview();

}
 @Override
 public void surfaceCreated(SurfaceHolder holder) {


 try {

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
          Camera.CameraInfo info=new Camera.CameraInfo();

          for (int i=0; i < Camera.getNumberOfCameras(); i++) {
            Camera.getCameraInfo(i, info);

            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
              camera=Camera.open(i);
              defaultCameraId = i;
            }
          }
        }

        if (camera == null) {
          camera=Camera.open();
        }




        try {
            camera.setPreviewDisplay(surfaceHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Camera.Parameters parameters = camera.getParameters();
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(defaultCameraId, info);
        int rotation = this.getWindowManager().getDefaultDisplay()
                .getRotation();
        if (Integer.parseInt(Build.VERSION.SDK) >= 8)
        {

             int degrees = 0;
             switch (rotation) {
                 case Surface.ROTATION_0: 
                     degrees = 0; break;
                 case Surface.ROTATION_90: 
                     degrees = 90; break;
                 case Surface.ROTATION_180: 
                     degrees = 180; break;
                 case Surface.ROTATION_270: 
                     degrees = 270; break;
             }
             int result;
             if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                 result = (info.orientation + degrees) % 360;
                 result = (360 - result) % 360;  
             } else {  // back-facing
                 result = (info.orientation - degrees + 360) % 360;
             }
             camera.setDisplayOrientation(result);

        }
        else
        {
            parameters.set("orientation", "portrait");
        }


        camera.setParameters(parameters);



} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 


}
 private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
            final double ASPECT_TOLERANCE = 0.1;
            double targetRatio = (double) w / h;
            if (sizes == null) return null;

            Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;

            int targetHeight = h;


            for (Size size : sizes) {
                double ratio = (double) size.width / size.height;
                if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }


            if (optimalSize == null) {
                minDiff = Double.MAX_VALUE;
                for (Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }
            }
            return optimalSize;
        }

解决方案

I also faced same issue in Samsung devices, later I implemented ExifInterface and solved successfully.

In any mode images will be shot it will always store in portrait mode only, and while fetching too returning in portrait mode. below of code I used to achieve my goal, I implemented within back camera, not sure about from camera.

Camera Intent@

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, 1212);   

onActivityResult@

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1212) {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
        Bitmap bitmap;
        //bitmap=GlobalMethods.decodeSampledBitmapFromResource(_path, 80, 80);
        bitmap=GlobalMethods.decodeFile(_path);
        if (bitmap == null) {
            imgMed.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.add_photo));
        } 
        else {
            imgMed.setImageBitmap(bitmap);
            imgMed.setScaleType(ScaleType.FIT_XY);

        }
    }
        }

decodeFile@

    public static Bitmap decodeFile(String path) {

          int orientation;

             try {

                 if(path==null){

                     return null;
                 }
                 // decode image size
                 BitmapFactory.Options o = new BitmapFactory.Options();
                 o.inJustDecodeBounds = true;

                 // Find the correct scale value. It should be the power of 2.
                 final int REQUIRED_SIZE = 70;
                 int width_tmp = o.outWidth, height_tmp = o.outHeight;
                 int scale = 4;
                 while (true) {
                     if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                         break;
                     width_tmp /= 2;
                     height_tmp /= 2;
                     scale++;
                 }
                 // decode with inSampleSize
                 BitmapFactory.Options o2 = new BitmapFactory.Options();
                 o2.inSampleSize=scale;
                 Bitmap bm = BitmapFactory.decodeFile(path,o2);


                 Bitmap bitmap = bm;

                 ExifInterface exif = new ExifInterface(path);
                 orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                 Log.e("orientation",""+orientation);
                 Matrix m=new Matrix();

                 if((orientation==3)){

                 m.postRotate(180);
                 m.postScale((float)bm.getWidth(), (float)bm.getHeight());

//               if(m.preRotate(90)){
                 Log.e("in orientation",""+orientation);

                 bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                 return  bitmap;
                 }
                 else if(orientation==6){

                  m.postRotate(90);

                  Log.e("in orientation",""+orientation);

                  bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                     return  bitmap;
                 }

                 else if(orientation==8){

                  m.postRotate(270);

                  Log.e("in orientation",""+orientation);

                  bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                     return  bitmap;
                 }
                 return bitmap;
             }
             catch (Exception e) {
             }
             return null;
         }