|
@@ -0,0 +1,485 @@
|
|
|
|
|
+package com.usai.redant.rautils.barcodescanner;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import android.Manifest;
|
|
|
|
|
+import android.annotation.SuppressLint;
|
|
|
|
|
+import android.app.Activity;
|
|
|
|
|
+import android.content.Intent;
|
|
|
|
|
+import android.graphics.Bitmap;
|
|
|
|
|
+import android.graphics.Rect;
|
|
|
|
|
+import android.media.Image;
|
|
|
|
|
+import android.os.Build;
|
|
|
|
|
+import android.os.Bundle;
|
|
|
|
|
+import android.util.Log;
|
|
|
|
|
+import android.util.Size;
|
|
|
|
|
+import android.view.View;
|
|
|
|
|
+import android.view.Window;
|
|
|
|
|
+import android.view.WindowManager;
|
|
|
|
|
+import android.widget.Button;
|
|
|
|
|
+
|
|
|
|
|
+import androidx.annotation.NonNull;
|
|
|
|
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
+import androidx.camera.core.AspectRatio;
|
|
|
|
|
+import androidx.camera.core.CameraSelector;
|
|
|
|
|
+import androidx.camera.core.ImageAnalysis;
|
|
|
|
|
+import androidx.camera.core.ImageCapture;
|
|
|
|
|
+import androidx.camera.core.ImageProxy;
|
|
|
|
|
+import androidx.camera.core.Preview;
|
|
|
|
|
+import androidx.camera.lifecycle.ProcessCameraProvider;
|
|
|
|
|
+import androidx.camera.view.PreviewView;
|
|
|
|
|
+import androidx.core.content.ContextCompat;
|
|
|
|
|
+
|
|
|
|
|
+import com.google.android.gms.tasks.OnCompleteListener;
|
|
|
|
|
+import com.google.android.gms.tasks.OnFailureListener;
|
|
|
|
|
+import com.google.android.gms.tasks.OnSuccessListener;
|
|
|
|
|
+import com.google.android.gms.tasks.Task;
|
|
|
|
|
+import com.google.common.util.concurrent.ListenableFuture;
|
|
|
|
|
+import com.google.mlkit.vision.barcode.BarcodeScanner;
|
|
|
|
|
+import com.google.mlkit.vision.barcode.BarcodeScannerOptions;
|
|
|
|
|
+import com.google.mlkit.vision.barcode.BarcodeScanning;
|
|
|
|
|
+import com.google.mlkit.vision.barcode.common.Barcode;
|
|
|
|
|
+import com.usai.redant.rautils.R;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
|
+
|
|
|
|
|
+public class CaptureActivityNew extends AppCompatActivity {
|
|
|
|
|
+
|
|
|
|
|
+//Button btn_photo;
|
|
|
|
|
+// Button btn_video;
|
|
|
|
|
+
|
|
|
|
|
+ private BeepManager beepManager;
|
|
|
|
|
+ Button swith_button;
|
|
|
|
|
+
|
|
|
|
|
+ boolean bscan = false;
|
|
|
|
|
+ private ImageCapture imageCapture = null;
|
|
|
|
|
+
|
|
|
|
|
+ private MaskViewNew mv ;
|
|
|
|
|
+// private VideoCapture<Recorder> videoCapture = null;
|
|
|
|
|
+// private Recording recording = null;
|
|
|
|
|
+
|
|
|
|
|
+ private ExecutorService cameraExecutor;
|
|
|
|
|
+ BarcodeScanner barcodeScanner;
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ Window window = getWindow();
|
|
|
|
|
+ window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
|
|
|
+
|
|
|
|
|
+ barcodeScanner =
|
|
|
|
|
+ BarcodeScanning.getClient(
|
|
|
|
|
+ new BarcodeScannerOptions.Builder()
|
|
|
|
|
+// .setBarcodeFormats(Barcode.FORMAT_PDF417)
|
|
|
|
|
+ .build());
|
|
|
|
|
+
|
|
|
|
|
+ setContentView(R.layout.activity_capture_new);
|
|
|
|
|
+
|
|
|
|
|
+ swith_button = (Button) findViewById(R.id.button);
|
|
|
|
|
+ swith_button.setTag("false");
|
|
|
|
|
+ swith_button.setBackground(getDrawable(R.drawable.ra_redbg_round_corner_25));
|
|
|
|
|
+ swith_button.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onClick(View v)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ // inactivityTimer.onPause();
|
|
|
|
|
+
|
|
|
|
|
+ if (Boolean.parseBoolean(v.getTag().toString()) == false)
|
|
|
|
|
+ {
|
|
|
|
|
+ // inactivityTimer.onResume();
|
|
|
|
|
+ v.setTag("true");
|
|
|
|
|
+ v.setBackground(getDrawable(R.drawable.ra_greenbg_round_corner_25));
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ // inactivityTimer.onPause();
|
|
|
|
|
+ v.setTag("false");
|
|
|
|
|
+ v.setBackground(getDrawable(R.drawable.ra_redbg_round_corner_25));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ mv = findViewById(R.id.maskview);
|
|
|
|
|
+ // 请求相机权限
|
|
|
|
|
+// if (allPermissionsGranted()) {
|
|
|
|
|
+ startCamera();
|
|
|
|
|
+// } else {
|
|
|
|
|
+// ActivityCompat.requestPermissions(this, Configuration.REQUIRED_PERMISSIONS,
|
|
|
|
|
+// Configuration.REQUEST_CODE_PERMISSIONS);
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ // 设置拍照按钮监听
|
|
|
|
|
+
|
|
|
|
|
+// btn_photo = findViewById(R.id.image_capture_button);
|
|
|
|
|
+// btn_video = findViewById(R.id.video_capture_button);
|
|
|
|
|
+
|
|
|
|
|
+// btn_photo.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
+// {
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onClick(View v)
|
|
|
|
|
+// {
|
|
|
|
|
+// takePhoto();
|
|
|
|
|
+// }
|
|
|
|
|
+// });
|
|
|
|
|
+// btn_video.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
+// {
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onClick(View v)
|
|
|
|
|
+// {
|
|
|
|
|
+// captureVideo();
|
|
|
|
|
+// }
|
|
|
|
|
+// });
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ cameraExecutor = Executors.newSingleThreadExecutor();
|
|
|
|
|
+
|
|
|
|
|
+ beepManager = new BeepManager(this);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void onResume()
|
|
|
|
|
+ {
|
|
|
|
|
+ super.onResume();
|
|
|
|
|
+
|
|
|
|
|
+ beepManager.updatePrefs();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // @SuppressLint("CheckResult")
|
|
|
|
|
+// private void captureVideo() {
|
|
|
|
|
+// // 确保videoCapture 已经被实例化,否则程序可能崩溃
|
|
|
|
|
+// if (videoCapture != null) {
|
|
|
|
|
+//
|
|
|
|
|
+//// btn_video.setEnabled(false);
|
|
|
|
|
+// Recording curRecording = recording;
|
|
|
|
|
+// if (curRecording != null) {
|
|
|
|
|
+// // 停止当前的 recording session(录制会话)
|
|
|
|
|
+// curRecording.stop();
|
|
|
|
|
+// recording = null;
|
|
|
|
|
+// return;
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// // 创建一个新的 recording session
|
|
|
|
|
+// String name = new SimpleDateFormat(Configuration.FILENAME_FORMAT, Locale.SIMPLIFIED_CHINESE)
|
|
|
|
|
+// .format(System.currentTimeMillis());
|
|
|
|
|
+// ContentValues contentValues = new ContentValues();
|
|
|
|
|
+// contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
|
|
|
|
|
+// contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
|
|
|
|
|
+// if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
|
|
|
|
|
+// contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/CameraX-Video");
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// MediaStoreOutputOptions mediaStoreOutputOptions = new MediaStoreOutputOptions
|
|
|
|
|
+// .Builder(getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
|
|
|
|
|
+// .setContentValues(contentValues)
|
|
|
|
|
+// .build();
|
|
|
|
|
+// // 申请音频权限
|
|
|
|
|
+// if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
+// ActivityCompat.requestPermissions(this,
|
|
|
|
|
+// new String[]{Manifest.permission.RECORD_AUDIO},
|
|
|
|
|
+// Configuration.REQUEST_CODE_PERMISSIONS);
|
|
|
|
|
+// }
|
|
|
|
|
+// recording = videoCapture.getOutput().prepareRecording(this, mediaStoreOutputOptions)
|
|
|
|
|
+// .withAudioEnabled() // 开启音频录制
|
|
|
|
|
+// .start(ContextCompat.getMainExecutor(this), videoRecordEvent -> {
|
|
|
|
|
+// if (videoRecordEvent instanceof VideoRecordEvent.Start) {
|
|
|
|
|
+//// btn_video.setEnabled(true);
|
|
|
|
|
+//// btn_video.setText("stop");
|
|
|
|
|
+// } else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
|
|
|
|
|
+// if (!((VideoRecordEvent.Finalize) videoRecordEvent).hasError()) {
|
|
|
|
|
+// String msg = "Video capture succeeded: " +
|
|
|
|
|
+// ((VideoRecordEvent.Finalize) videoRecordEvent).getOutputResults()
|
|
|
|
|
+// .getOutputUri();
|
|
|
|
|
+// Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
|
|
|
|
|
+// Log.d(Configuration.TAG, msg);
|
|
|
|
|
+// } else {
|
|
|
|
|
+// if (recording != null) {
|
|
|
|
|
+// recording.close();
|
|
|
|
|
+// recording = null;
|
|
|
|
|
+// Log.e(Configuration.TAG, "Video capture end with error: " +
|
|
|
|
|
+// ((VideoRecordEvent.Finalize) videoRecordEvent).getError());
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+//// btn_video.setEnabled(false);
|
|
|
|
|
+//// btn_video.setText("start capture");
|
|
|
|
|
+//
|
|
|
|
|
+// }
|
|
|
|
|
+// });
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+// private void takePhoto() {
|
|
|
|
|
+// // 确保imageCapture 已经被实例化, 否则程序将可能崩溃
|
|
|
|
|
+// if (imageCapture != null) {
|
|
|
|
|
+// // 创建带时间戳的输出文件以保存图片,带时间戳是为了保证文件名唯一
|
|
|
|
|
+// String name = new SimpleDateFormat(Configuration.FILENAME_FORMAT,
|
|
|
|
|
+// Locale.SIMPLIFIED_CHINESE).format(System.currentTimeMillis());
|
|
|
|
|
+// ContentValues contentValues = new ContentValues();
|
|
|
|
|
+// contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
|
|
|
|
|
+// contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
|
|
|
|
|
+// if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
|
|
|
|
|
+// contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Image");
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// // 创建 output option 对象,用以指定照片的输出方式。
|
|
|
|
|
+// // 在这个对象中指定有关我们希望输出如何的方式。我们希望将输出保存在 MediaStore 中,以便其他应用可以显示它
|
|
|
|
|
+// ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions
|
|
|
|
|
+// .Builder(getContentResolver(),
|
|
|
|
|
+// MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
|
|
|
|
+// contentValues)
|
|
|
|
|
+// .build();
|
|
|
|
|
+//
|
|
|
|
|
+// // 设置拍照监听,用以在照片拍摄后执行takePicture(拍照)方法
|
|
|
|
|
+// imageCapture.takePicture(outputFileOptions,
|
|
|
|
|
+// ContextCompat.getMainExecutor(this),
|
|
|
|
|
+// new ImageCapture.OnImageSavedCallback() {// 保存照片时的回调
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
|
|
|
|
|
+// String msg = "照片捕获成功! " + outputFileResults.getSavedUri();
|
|
|
|
|
+// Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
|
|
|
|
|
+// Log.d(Configuration.TAG, msg);
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onError(@NonNull ImageCaptureException exception) {
|
|
|
|
|
+// Log.e(Configuration.TAG, "Photo capture failed: " + exception.getMessage());
|
|
|
|
|
+// }
|
|
|
|
|
+// });
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ private void startCamera() {
|
|
|
|
|
+ // 将Camera的生命周期和Activity绑定在一起(设定生命周期所有者),这样就不用手动控制相机的启动和关闭。
|
|
|
|
|
+ ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
|
|
|
|
|
+
|
|
|
|
|
+ cameraProviderFuture.addListener(() -> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 将你的相机和当前生命周期的所有者绑定所需的对象
|
|
|
|
|
+ ProcessCameraProvider processCameraProvider = cameraProviderFuture.get();
|
|
|
|
|
+
|
|
|
|
|
+ // 创建一个Preview 实例,并设置该实例的 surface 提供者(provider)。
|
|
|
|
|
+ Preview preview = new Preview.Builder()
|
|
|
|
|
+ .setTargetResolution(new Size(1920,1080))
|
|
|
|
|
+
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ PreviewView v = findViewById(R.id.viewFinder);
|
|
|
|
|
+ preview.setSurfaceProvider(v.getSurfaceProvider());
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+// ResolutionInfo rinfo=preview.getResolutionInfo();
|
|
|
|
|
+
|
|
|
|
|
+// // 创建录像所需实例
|
|
|
|
|
+// Recorder recorder = new Recorder.Builder()
|
|
|
|
|
+// .setQualitySelector(QualitySelector.from(Quality.HIGHEST))
|
|
|
|
|
+// .build();
|
|
|
|
|
+// videoCapture = VideoCapture.withOutput(recorder);
|
|
|
|
|
+
|
|
|
|
|
+ // 选择后置摄像头作为默认摄像头
|
|
|
|
|
+ CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;
|
|
|
|
|
+
|
|
|
|
|
+ // 创建拍照所需的实例
|
|
|
|
|
+ imageCapture = new ImageCapture.Builder().build();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 设置预览帧分析
|
|
|
|
|
+ ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
|
|
|
|
|
+// .setTargetResolution(new Size(1920,1080))
|
|
|
|
|
+ .setTargetAspectRatio(AspectRatio.RATIO_16_9)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ imageAnalysis.setAnalyzer(cameraExecutor, new MyAnalyzer());
|
|
|
|
|
+
|
|
|
|
|
+ // 重新绑定用例前先解绑
|
|
|
|
|
+ processCameraProvider.unbindAll();
|
|
|
|
|
+
|
|
|
|
|
+ // 绑定用例至相机
|
|
|
|
|
+ /*此步骤具有 Camera2 设备文档中指定的设备级别要求:
|
|
|
|
|
+ 预览 + 视频捕获 + 图像捕获:LIMITED设备及以上。
|
|
|
|
|
+ 预览 + 视频捕获 + 图像分析:LEVEL_3(最高)设备添加到 Android 7(N)。
|
|
|
|
|
+ 预览 + 视频捕获 + 图像分析 + 图像捕获:不支持。
|
|
|
|
|
+ */
|
|
|
|
|
+ processCameraProvider.bindToLifecycle(CaptureActivityNew.this, cameraSelector,
|
|
|
|
|
+ preview,
|
|
|
|
|
+// imageCapture,
|
|
|
|
|
+ imageAnalysis/*,
|
|
|
|
|
+ videoCapture*/);
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ Log.e(Configuration.TAG, "用例绑定失败!" + e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, ContextCompat.getMainExecutor(this));
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// private boolean allPermissionsGranted() {
|
|
|
|
|
+// for (String permission : Configuration.REQUIRED_PERMISSIONS) {
|
|
|
|
|
+// if (ContextCompat.checkSelfPermission(this, permission)
|
|
|
|
|
+// != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
+// return false;
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// return true;
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
|
+// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
+// if (requestCode == Configuration.REQUEST_CODE_PERMISSIONS) {
|
|
|
|
|
+// if (allPermissionsGranted()) {// 申请权限通过
|
|
|
|
|
+// startCamera();
|
|
|
|
|
+// } else {// 申请权限失败
|
|
|
|
|
+// Toast.makeText(this, "用户拒绝授予权限!", Toast.LENGTH_LONG).show();
|
|
|
|
|
+// finish();
|
|
|
|
|
+// }
|
|
|
|
|
+// } else if (requestCode == Configuration.REQUEST_AUDIO_CODE_PERMISSIONS) {
|
|
|
|
|
+// if (ContextCompat.checkSelfPermission(this,
|
|
|
|
|
+// "Manifest.permission.RECORD_AUDIO") != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
+// Toast.makeText(this, "未授权录制音频权限!", Toast.LENGTH_LONG).show();
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void onDestroy() {
|
|
|
|
|
+ super.onDestroy();
|
|
|
|
|
+ cameraExecutor.shutdown();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static class Configuration {
|
|
|
|
|
+ public static final String TAG = "CameraxBasic";
|
|
|
|
|
+ public static final String FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS";
|
|
|
|
|
+ public static final int REQUEST_CODE_PERMISSIONS = 10;
|
|
|
|
|
+ public static final int REQUEST_AUDIO_CODE_PERMISSIONS = 12;
|
|
|
|
|
+ public static final String[] REQUIRED_PERMISSIONS =
|
|
|
|
|
+ Build.VERSION.SDK_INT <= Build.VERSION_CODES.P ?
|
|
|
|
|
+ new String[]{Manifest.permission.CAMERA,
|
|
|
|
|
+ Manifest.permission.RECORD_AUDIO,
|
|
|
|
|
+ Manifest.permission.WRITE_EXTERNAL_STORAGE} :
|
|
|
|
|
+ new String[]{Manifest.permission.CAMERA,
|
|
|
|
|
+ Manifest.permission.RECORD_AUDIO};
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private class MyAnalyzer implements ImageAnalysis.Analyzer{
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressLint("UnsafeOptInUsageError")
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void analyze(@NonNull ImageProxy image) {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ String tag = swith_button.getTag().toString();
|
|
|
|
|
+ if (!Boolean.parseBoolean(tag))
|
|
|
|
|
+ {
|
|
|
|
|
+ image.close();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Log.e("barcode","barcode: begin");
|
|
|
|
|
+// Log.e(Configuration.TAG, "Image's stamp is " + Objects.requireNonNull(image.getImage()).getTimestamp());
|
|
|
|
|
+
|
|
|
|
|
+ double ratio = mv.getRatio();
|
|
|
|
|
+
|
|
|
|
|
+ Image img=image.getImage();
|
|
|
|
|
+
|
|
|
|
|
+ int width = (int) (img.getWidth() * ratio);
|
|
|
|
|
+ int height = (int) (img.getHeight() * ratio);
|
|
|
|
|
+
|
|
|
|
|
+ int left = (img.getWidth() - width) / 2;
|
|
|
|
|
+ int top = (img.getHeight() - height) / 2;
|
|
|
|
|
+ int right = width + left;
|
|
|
|
|
+ int bottom = height + top;
|
|
|
|
|
+
|
|
|
|
|
+ Rect frame = new Rect();
|
|
|
|
|
+ frame.left = left;
|
|
|
|
|
+ frame.top = top;
|
|
|
|
|
+ frame.right = right;
|
|
|
|
|
+ frame.bottom = bottom;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ image.setCropRect(frame);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ Bitmap bitmapImage = image.toBitmap();
|
|
|
|
|
+
|
|
|
|
|
+ Bitmap cbitmap = Bitmap.createBitmap(bitmapImage, left , top, width, height, null, false);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+// Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null, false);
|
|
|
|
|
+// Image imgc = img.crop
|
|
|
|
|
+// ImageInfo info = image.getImageInfo();
|
|
|
|
|
+
|
|
|
|
|
+// img.setCropRect();
|
|
|
|
|
+
|
|
|
|
|
+// InputImage i = InputImage.fromMediaImage(image.getImage(), image.getImageInfo().getRotationDegrees());
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+// Task<List<Barcode>> task = barcodeScanner.process(i);
|
|
|
|
|
+
|
|
|
|
|
+ Task<List<Barcode>> task =barcodeScanner.process(cbitmap,image.getImageInfo().getRotationDegrees());
|
|
|
|
|
+ task.addOnSuccessListener(new OnSuccessListener<List<Barcode>>()
|
|
|
|
|
+ {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onSuccess(List<Barcode> barcodes)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ if(barcodes.size()<1)
|
|
|
|
|
+ return;
|
|
|
|
|
+//
|
|
|
|
|
+ beepManager.playBeepSoundAndVibrate();
|
|
|
|
|
+ swith_button.setTag("false");
|
|
|
|
|
+ Log.e("barcodes","count:"+barcodes.size());
|
|
|
|
|
+// for (int i = 0; i < barcodes.size(); ++i)
|
|
|
|
|
+ {
|
|
|
|
|
+ Barcode barcode = barcodes.get(0);
|
|
|
|
|
+
|
|
|
|
|
+// image.close();
|
|
|
|
|
+ Log.e("barcode","barcode:"+barcode.getDisplayValue());
|
|
|
|
|
+
|
|
|
|
|
+ Intent result = new Intent();
|
|
|
|
|
+ result.putExtra("pid", barcode.getDisplayValue());
|
|
|
|
|
+ setResult(Activity.RESULT_OK, result);
|
|
|
|
|
+ finish();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ task.addOnFailureListener(new OnFailureListener()
|
|
|
|
|
+ {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onFailure(@NonNull Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+// Log.e("barcode","barcode: failed");
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ task.addOnCompleteListener(new OnCompleteListener<List<Barcode>>()
|
|
|
|
|
+ {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onComplete(@NonNull Task<List<Barcode>> task)
|
|
|
|
|
+ {
|
|
|
|
|
+ image.close();
|
|
|
|
|
+// Log.e("barcode","barcode: complete");
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+// task.addOnSuccessListener(...).addOnFailureListener(...);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|