所以我试图用google ML Kit创建一个条形码扫描仪,它使用android内置的相机扫描二维码,但是FirebasevisionImage类需要一个media.image,我不知道如何创建它。这是我的代码。
公共类 QrActivity 扩展 AppCompatActivity 实现 SurfaceHolder.Callback{
private static final String TAG = "QrActivity";
SurfaceView surfaceView = findViewById(R.id.cameraView);
SurfaceHolder holder = surfaceView.getHolder();
CameraManager cameraManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
surfaceHolder = this.holder ;
try {
Image myimage = cameraManager.openCamera(1,);
getRotationCompensation(getCameraId(),QrActivity.this,getApplicationContext());
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,1);
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(reader, getRotationCompensation(getCameraId(),QrActivity.this,getApplicationContext()));
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
还有一个方法来获取和返回相机 id 并创建一个 sparseInt 数组来获取相机旋转。
private String getCameraId(){
String mCameraId = null;
CameraManager cameraManager =(CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
for(String cameraId:cameraManager.getCameraIdList()){
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING)==1){
mCameraId = cameraId;
return mCameraId;
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
return mCameraId;
}
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
但我想知道的是如何使条形码扫描仪与设备相机配合使用。
参考https://firebase.google.com/docs/ml-kit/android/read-barcodes