|
|
@@ -4,7 +4,9 @@ import android.content.Intent;
|
|
|
import android.graphics.Bitmap;
|
|
|
import android.graphics.PointF;
|
|
|
import android.graphics.RectF;
|
|
|
+import android.media.MediaScannerConnection;
|
|
|
import android.net.Uri;
|
|
|
+import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Environment;
|
|
|
import android.os.Handler;
|
|
|
@@ -13,9 +15,13 @@ import android.view.View;
|
|
|
import android.widget.ImageView;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.core.content.FileProvider;
|
|
|
|
|
|
import com.miekir.ocr.R;
|
|
|
import com.miekir.ocr.base.BaseCameraActivity;
|
|
|
+import com.miekir.ocr.tool.ImageOrientation;
|
|
|
+import com.miekir.ocr.tool.ImageUtil;
|
|
|
import com.otaliastudios.cameraview.CameraListener;
|
|
|
import com.otaliastudios.cameraview.CameraLogger;
|
|
|
import com.otaliastudios.cameraview.CameraOptions;
|
|
|
@@ -32,8 +38,7 @@ import com.otaliastudios.cameraview.frame.Frame;
|
|
|
import com.otaliastudios.cameraview.frame.FrameProcessor;
|
|
|
|
|
|
import java.io.File;
|
|
|
-
|
|
|
-import rx_activity_result2.RxActivityResult;
|
|
|
+import java.io.IOException;
|
|
|
|
|
|
import static android.os.Environment.DIRECTORY_PICTURES;
|
|
|
|
|
|
@@ -180,84 +185,108 @@ public class OtaCameraActivity extends BaseCameraActivity {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private File tempRawPhotoFile;
|
|
|
/**
|
|
|
* 打开原生相机
|
|
|
*/
|
|
|
private void openRawCamera() {
|
|
|
- File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES) + "/" + System.currentTimeMillis() + ".jpg");
|
|
|
- if (!file.getParentFile().exists()) {
|
|
|
- file.getParentFile().mkdirs();
|
|
|
+ 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不一样
|
|
|
- /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
//android7.0以上
|
|
|
- ContentValues contentValues = new ContentValues(1);
|
|
|
- String absolutePath = file.getAbsolutePath();
|
|
|
- contentValues.put(MediaStore.Images.Media.DATA, absolutePath);
|
|
|
- mImageCaptureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
|
|
|
+ mImageCaptureUri = getNewUri(tempRawPhotoFile);
|
|
|
} else {
|
|
|
- mImageCaptureUri = Uri.fromFile(file);
|
|
|
- }*/
|
|
|
- mImageCaptureUri = Uri.fromFile(file);
|
|
|
+ mImageCaptureUri = Uri.fromFile(tempRawPhotoFile);
|
|
|
+ }
|
|
|
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
|
|
|
|
|
|
camera.close();
|
|
|
- RxActivityResult.on(this)
|
|
|
- .startIntent(takeIntent)
|
|
|
- //.filter(result -> result.resultCode() == RESULT_OK)
|
|
|
- //.map(result -> result.data().getData())
|
|
|
- .doOnNext(data -> {
|
|
|
- if (data.resultCode() == RESULT_OK) {
|
|
|
- cropPhoto(file);
|
|
|
- } else {
|
|
|
- new Handler().postDelayed(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- camera.open();
|
|
|
- }
|
|
|
- }, 1000);
|
|
|
- }
|
|
|
- })
|
|
|
- .subscribe();
|
|
|
+ new Handler().postDelayed(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ startActivityForResult(takeIntent, 6);
|
|
|
+ }
|
|
|
+ }, 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+ if (resultCode == RESULT_OK) {
|
|
|
+ if (requestCode == 6) {
|
|
|
+ correctPhotoRotation(tempRawPhotoFile);
|
|
|
+ // 通知相册去获取
|
|
|
+ //MediaScannerConnection.scanFile(this, new String[] { tempRawPhotoFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
|
|
|
+ //cropPhoto(tempRawPhotoFile, getNewUri(tempRawPhotoFile));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (requestCode == 9) {
|
|
|
+ // 通知相册去获取
|
|
|
+ MediaScannerConnection.scanFile(this, new String[] { croppedFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
|
|
|
+ onOppoResult(croppedPhotoUri);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ new Handler().postDelayed(() -> camera.open(), 1000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Uri getNewUri(File file) {
|
|
|
+ return FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", file);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void correctPhotoRotation(File imageFile) {
|
|
|
+ Bitmap rawBitmap = ImageUtil.getBitmapFromUri(this, Uri.fromFile(imageFile));
|
|
|
+ boolean correctSuccess = true;
|
|
|
+ try {
|
|
|
+ rawBitmap = ImageOrientation.modifyOrientation(imageFile.getAbsolutePath(), rawBitmap);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ correctSuccess = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (correctSuccess) {
|
|
|
+ boolean saveSuccess = ImageUtil.saveBitmapFile(rawBitmap, imageFile);
|
|
|
+ if (saveSuccess) {
|
|
|
+ // 通知相册去获取
|
|
|
+ MediaScannerConnection.scanFile(this, new String[] { tempRawPhotoFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
|
|
|
+ cropPhoto(tempRawPhotoFile, getNewUri(tempRawPhotoFile));
|
|
|
+ } else {
|
|
|
+ camera.open();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ camera.open();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void cropPhoto(File file) {
|
|
|
- File cropFile = new File(file.getParentFile().getAbsolutePath(), "crop_" + file.getName());
|
|
|
- Uri cropUri = Uri.fromFile(cropFile);
|
|
|
+ private Uri croppedPhotoUri;
|
|
|
+ private File croppedFile;
|
|
|
+ private void cropPhoto(File file, Uri rawFileUri) {
|
|
|
+ croppedFile = new File(file.getParentFile().getAbsolutePath(), "crop_" + file.getName());
|
|
|
+ // 这里必须用这个,否则无法裁剪
|
|
|
+ croppedPhotoUri = Uri.fromFile(croppedFile);
|
|
|
Intent intent = new Intent("com.android.camera.action.CROP");
|
|
|
- intent.setDataAndType(Uri.fromFile(file), "image/*");
|
|
|
+ intent.setDataAndType(rawFileUri, "image/*");
|
|
|
intent.putExtra("crop", "true");//可裁剪
|
|
|
- intent.putExtra("aspectX", 1); //裁剪的宽比例
|
|
|
- intent.putExtra("aspectY", 1); //裁剪的高比例
|
|
|
+ //intent.putExtra("aspectX", 1); //裁剪的宽比例
|
|
|
+ //intent.putExtra("aspectY", 1); //裁剪的高比例
|
|
|
intent.putExtra("outputX", mRight-mLeft); //裁剪的宽度
|
|
|
intent.putExtra("outputY", mBottom-mTop); //裁剪的高度
|
|
|
- intent.putExtra("scale", true); //支持缩放
|
|
|
- intent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri); //将裁剪的结果输出到指定的Uri
|
|
|
+ intent.putExtra("scale", false); //不限死比例
|
|
|
+ intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedPhotoUri); //将裁剪的结果输出到指定的Uri
|
|
|
intent.putExtra("return-data", true); //若为true则表示返回数据
|
|
|
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//裁剪成的图片的格式
|
|
|
+ // 让系统打开文件
|
|
|
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
|
|
camera.close();
|
|
|
- RxActivityResult.on(this)
|
|
|
- .startIntent(intent)
|
|
|
- //.filter(result -> result.resultCode() == RESULT_OK)
|
|
|
- //.map(result -> result.data().getData())
|
|
|
- .doOnNext(data -> {
|
|
|
- if (data.resultCode() == RESULT_OK) {
|
|
|
- onOppoResult(cropUri);
|
|
|
- } else {
|
|
|
- new Handler().postDelayed(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- camera.open();
|
|
|
- }
|
|
|
- }, 1000);
|
|
|
- }
|
|
|
- })
|
|
|
- .subscribe();
|
|
|
+ startActivityForResult(intent, 9);
|
|
|
}
|
|
|
|
|
|
protected void onOppoResult(Uri uri) {
|