我正在尝试使用新的CameraX api来简单地拍摄图像,而每第二张图像(如第一张图像就好,第二张图像就不好,第三张图像就好,第四张图像就不好了)变成绿色背景(请参见上图)
private void startCamera() {
CameraX.unbindAll();
int aspRatioW = textureView.getWidth();
int aspRatioH = textureView.getHeight();
Rational asp = new Rational (aspRatioW, aspRatioH);
PreviewConfig previewConfig=new PreviewConfig.Builder()
.setLensFacing(lensFacing)
.build();
preview=new Preview(previewConfig);
preview.setOnPreviewOutputUpdateListener(new Preview.OnPreviewOutputUpdateListener() {
@Override
public void onUpdated(Preview.PreviewOutput output) {
ViewGroup viewGroup=(ViewGroup) textureView.getParent();
viewGroup.removeView(textureView);
viewGroup.addView(textureView, 0);
textureView.setSurfaceTexture(output.getSurfaceTexture());
updateTransform();
}
});
ImageCaptureConfig imageCaptureConfig=new ImageCaptureConfig.Builder()
.setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
.setTargetRotation(getWindowManager().getDefaultDisplay().getRotation())
.setTargetAspectRatio(asp)
.build();
final ImageCapture imgCap=new ImageCapture(imageCaptureConfig);
findViewById(R.id.camera_photoButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file=new File(Environment.getExternalStorageDirectory()+"/"+"Prixpi_"+System.currentTimeMillis()
+".jpg");
imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
@Override
public void onImageSaved(@NonNull File file) {
String path=file.getAbsolutePath();
ScanImagePath obj=new ScanImagePath(path);
Log.d(TAG, "onImageSaved: path="+obj.getImagePath());
mRepo.insertImageTask(obj);
Toast.makeText(CameraActivity.this, "Image captured", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {
String msg = "Photo capture failed: " + message;
Toast.makeText(CameraActivity.this, msg,Toast.LENGTH_LONG).show();
if(cause != null){
cause.printStackTrace();
}
}
});
}
});
CameraX.bindToLifecycle((LifecycleOwner)this, imgCap, preview);
}
不确定我做错了什么,我们将不胜感激