2017-07-03 135 views
0

我想推出本機Android相機並將拍攝的圖像保存在指定的位置,但是當我這樣做,並且我稍後嘗試拍攝照片時,保存的圖像本身可以旋轉90度。我正在使用LG手機來運行該程序。我已經嘗試了我在這裏找到的解決方案,但他們沒有工作。我把我的代碼放在這裏......請糾正我或給我一個新的解決方案。 非常感謝Android旋轉相機拍攝的照片本身

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

它不會在所有設備中旋轉。您可以嘗試this解決方案。我曾經做過類似的事情,這對我很合適。

相關問題