EXIF orientation tag value always 0 for image taken with portrait camera app android

前端 未结 4 506
南方客
南方客 2020-11-27 16:24

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 val

4条回答
  •  被撕碎了的回忆
    2020-11-27 17:02

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

提交回复
热议问题