Android自动旋转相机拍摄的照片

时间:2017-07-03 09:01:25

标签: java android

我想启动原生Android相机并将拍摄的图像保存在指定位置,但是当我这样做并且我稍后尝试到达照片时,保存的图像会自动旋转90度。我正在使用LG手机来运行该程序。我已经尝试过我在这里找到的解决方案,但它们没有用。我在这里为你提供代码......请纠正我或给我一个新的解决方案。 非常感谢你

 private void cameraIntent() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       intent.putExtra("photo", true);
      intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
     intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
     intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
      startActivityForResult(intent, REQUEST_CAMERA);
    }

     public void onActivityResult(int requestCode, int resultCode, Intent data) {

            super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == Activity.RESULT_OK) {
              if (requestCode == REQUEST_CAMERA)
                  onCaptureImageResult(data);
            }
    }



    private void onCaptureImageResult(Intent data) {


      ByteArrayOutputStream bytes = null;
             Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

                    bytes = new ByteArrayOutputStream();
                    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

                File destination = new File(getExternalStorageDirectory(),
                        File.separator + "profile.jpg");

                FileOutputStream fo;
                try {
                    destination.createNewFile();
                    fo = new FileOutputStream(destination);
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            String address = getExternalStorageDirectory().getAbsolutePath();
     ExifInterface   exif = null;
            try {
                if(destination.exists()) {
                      exif = new ExifInterface(address+File.separator+"profile.jpg");

                    Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }



            if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
                thumbnail= rotate(thumbnail, 90);
            }else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
                thumbnail= rotate(thumbnail, 270);
            }else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
                thumbnail= rotate(thumbnail, 270);

            }



            resized = Bitmap.createScaledBitmap(thumbnail, navUserImage.getWidth(), navUserImage.getHeight(), true);
          navUserImage.setImageBitmap(resized);
    }

1 个答案:

答案 0 :(得分:1)

它不会在所有设备中旋转。您可以尝试this解决方案。我已经做过类似的事了,它很适合我。