|
@@ -100,6 +100,7 @@ import androidx.core.app.ActivityCompat;
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
|
|
+import android.util.Size;
|
|
|
import static net.posprinter.TSPLConst.FNT_SIMPLIFIED_CHINESE;
|
|
|
import static net.posprinter.TSPLConst.ROTATION_90;
|
|
|
|
|
@@ -747,6 +748,8 @@ public class MainActivity extends BaseActivity {
|
|
|
// -------------------------------------获取usb口状态结束---------------------------------
|
|
|
|
|
|
// -------------------------------------拍照相关方法结束----------------------------------
|
|
|
+ int retryCount = 0;
|
|
|
+ int maxRetries = 10;
|
|
|
private void toCloseCamera() {
|
|
|
// 如果相机预览还开着, 则关闭
|
|
|
cameraIsOpening = false;
|
|
@@ -824,8 +827,10 @@ public class MainActivity extends BaseActivity {
|
|
|
@SuppressLint("RestrictedApi")
|
|
|
@Override
|
|
|
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
|
|
|
- Log.e("-------->","------>savePath="+outputFileResults.getSavedUri().getPath());
|
|
|
- Log.e("-------->","------>保存成功");
|
|
|
+ Log.e("takePhoto","------>savePath="+outputFileResults.getSavedUri().getPath());
|
|
|
+ Log.e("takePhoto","------>保存成功");
|
|
|
+ Log.e("takePhoto","------>重试数:" + retryCount);
|
|
|
+ retryCount = 0;
|
|
|
// Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
|
|
|
savePicture(file, path, name);
|
|
|
mWebView.loadUrl("javascript:uploadPhotoPath('" + outputFileOptions.getFile().getAbsolutePath() + "')");
|
|
@@ -833,8 +838,19 @@ public class MainActivity extends BaseActivity {
|
|
|
|
|
|
@Override
|
|
|
public void onError(@NonNull ImageCaptureException exception) {
|
|
|
- Log.e("----------->","------->"+exception.getMessage());
|
|
|
-// Toast.makeText(MainActivity.this, "Error: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
+ Log.e("takePhoto","------>保存失败: "+exception.getMessage());
|
|
|
+ Log.e("takePhoto","------>重试数:" + retryCount);
|
|
|
+ if (retryCount < maxRetries) {
|
|
|
+ retryCount++;
|
|
|
+ // 重试拍照
|
|
|
+ imageCapture.takePicture(
|
|
|
+ // 第一个参数 OutputFileOptions 指定了照片保存的位置和格式等信息。
|
|
|
+ outputFileOptions,
|
|
|
+ // 第二个参数 Executor 指定了保存照片时要运行的线程。
|
|
|
+ ContextCompat.getMainExecutor(MainActivity.this),
|
|
|
+ this
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -907,6 +923,8 @@ public class MainActivity extends BaseActivity {
|
|
|
videoCapture = null;
|
|
|
imageCapture = new ImageCapture.Builder()
|
|
|
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
|
|
|
+ .setTargetResolution(new Size(1280, 720))
|
|
|
+ .setFlashMode(ImageCapture.FLASH_MODE_OFF) // 关闭闪光灯
|
|
|
// 设置JPEG的质量为85%
|
|
|
.setJpegQuality(85)
|
|
|
.build();
|