詹子聪 5 年之前
父節點
當前提交
345369d798

+ 41 - 0
app/src/main/java/com/miekir/ocr/tool/ImageOrientation.java

@@ -12,6 +12,8 @@ import android.os.Environment;
 import android.provider.DocumentsContract;
 import android.provider.MediaStore;
 
+import com.miekir.ocr.OCRApplication;
+
 import java.io.IOException;
 
 public class ImageOrientation {
@@ -42,6 +44,45 @@ public class ImageOrientation {
         }
     }
 
+    public static Bitmap modifyOrientation(int orientation, Bitmap bitmap) throws IOException {
+        switch (orientation) {
+            case ExifInterface.ORIENTATION_ROTATE_90:
+                return rotate(bitmap, 90);
+
+            case ExifInterface.ORIENTATION_ROTATE_180:
+                return rotate(bitmap, 180);
+
+            case ExifInterface.ORIENTATION_ROTATE_270:
+                return rotate(bitmap, 270);
+
+            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
+                return flip(bitmap, true, false);
+
+            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
+                return flip(bitmap, false, true);
+
+            default:
+                return bitmap;
+        }
+    }
+
+    private static int getRotationFromMedia(Uri imageUri) {
+            if(imageUri == null) {
+                return 0;
+            }
+
+            String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION};
+            Cursor cursor = OCRApplication.getsInstance().getContentResolver().query(imageUri, projection, null, null, null);
+
+            int orientation = -1;
+            if (cursor != null && cursor.moveToFirst()) {
+                orientation = cursor.getInt(0);
+                cursor.close();
+            }
+
+            return orientation;
+    }
+
     public static String getPath(final Context context, final Uri uri) {
 
         final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

+ 17 - 8
app/src/main/java/com/miekir/ocr/ui/camera/otalia/OtaCameraActivity.java

@@ -185,17 +185,23 @@ public class OtaCameraActivity extends BaseCameraActivity {
     }
 
 
+    private boolean mIsRawOpen = false;
     private File tempRawPhotoFile;
+    // 保存拍照的路径
+    private Uri mImageCaptureUri;
     /**
      * 打开原生相机
      */
     private void openRawCamera() {
+        if (mIsRawOpen) {
+            return;
+        }
+        mIsRawOpen = true;
         tempRawPhotoFile = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES) + "/" + System.currentTimeMillis() + ".jpg");
         if (!tempRawPhotoFile.getParentFile().exists()) {
             tempRawPhotoFile.getParentFile().mkdirs();
         }
-        // 保存拍照的路径
-        Uri mImageCaptureUri;
+
         // 跳转系统相机的intent
         Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         // 根据不同的Android版本uri不一样
@@ -205,6 +211,7 @@ public class OtaCameraActivity extends BaseCameraActivity {
         } else {
             mImageCaptureUri = Uri.fromFile(tempRawPhotoFile);
         }
+
         takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
 
         camera.close();
@@ -219,9 +226,10 @@ public class OtaCameraActivity extends BaseCameraActivity {
     @Override
     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
+        mIsRawOpen = false;
         if (resultCode == RESULT_OK) {
             if (requestCode == 6) {
-                correctPhotoRotation(tempRawPhotoFile);
+                correctPhotoRotation(tempRawPhotoFile, mImageCaptureUri);
                 // 通知相册去获取
                 //MediaScannerConnection.scanFile(this, new String[] { tempRawPhotoFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
                 //cropPhoto(tempRawPhotoFile, getNewUri(tempRawPhotoFile));
@@ -241,8 +249,9 @@ public class OtaCameraActivity extends BaseCameraActivity {
         return FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", file);
     }
 
-    private void correctPhotoRotation(File imageFile) {
-        Bitmap rawBitmap = ImageUtil.getBitmapFromUri(this, Uri.fromFile(imageFile));
+    private void correctPhotoRotation(File imageFile, Uri imageUri) {
+        //Bitmap rawBitmap = ImageUtil.getBitmapFromUri(this, Uri.fromFile(imageFile));
+        Bitmap rawBitmap = ImageUtil.getBitmapFromUri(this, mImageCaptureUri);
         boolean correctSuccess = true;
         try {
             rawBitmap = ImageOrientation.modifyOrientation(imageFile.getAbsolutePath(), rawBitmap);
@@ -276,9 +285,9 @@ public class OtaCameraActivity extends BaseCameraActivity {
         intent.putExtra("crop", "true");//可裁剪
         //intent.putExtra("aspectX", 1); //裁剪的宽比例
         //intent.putExtra("aspectY", 1);  //裁剪的高比例
-        intent.putExtra("outputX", mRight-mLeft); //裁剪的宽度
-        intent.putExtra("outputY", mBottom-mTop);  //裁剪的高度
-        intent.putExtra("scale", false); //不限死比例
+        //intent.putExtra("outputX", mRight-mLeft); //裁剪的宽度
+        //intent.putExtra("outputY", mBottom-mTop);  //裁剪的高度
+        intent.putExtra("scale", true); //可以改变裁剪大小
         intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedPhotoUri);  //将裁剪的结果输出到指定的Uri
         intent.putExtra("return-data", true); //若为true则表示返回数据
         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//裁剪成的图片的格式