在Android相机应用中,OCR(Optical Character Recognition,光学字符识别)和从预览中拍照之间可能会产生冲突,因为两者需要同时访问相机预览数据。以下是一个解决这个冲突的示例代码:
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.googlecode.tesseract.android.TessBaseAPI;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PreviewCallback {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 100;
private static final String TAG = "MainActivity";
private Camera camera;
private SurfaceHolder surfaceHolder;
private TessBaseAPI tessBaseAPI;
private Button captureButton;
private ImageView capturedImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SurfaceView surfaceView = findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
captureButton = findViewById(R.id.captureButton);
capturedImageView = findViewById(R.id.capturedImageView);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
captureImage();
}
});
// 初始化OCR引擎
tessBaseAPI = new TessBaseAPI();
tessBaseAPI.init(getFilesDir().getAbsolutePath(), "eng");
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCamera();
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
startCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (surfaceHolder.getSurface() == null) {
return;
}
try {
camera.stopPreview();
} catch (Exception e) {
Log.e(TAG, "Error stopping camera preview: " + e.getMessage());
}
try {
camera.setPreviewDisplay(surfaceHolder);
camera.setPreviewCallback(this);
camera.startPreview();
} catch (IOException e) {
Log.e(TAG, "Error starting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// 在这里执行OCR识别逻辑,可以使用tessBaseAPI来处理相机预览数据
}
private void startCamera() {
try {
camera = Camera.open();
} catch (Exception e) {
Log.e(TAG, "Error opening camera: " + e.getMessage());
}
}
private void releaseCamera() {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
private void captureImage() {
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
capturedImageView.setImageBitmap(bitmap);
camera.startPreview();
}
});
}
}
这个示例代码中,通过SurfaceView来显示相机预览,并使用SurfaceHolder.Callback来处理Surface的创建、变化和销毁事件。在surfaceChanged
方法中,我们将设置相机预览的Surface,并注册Camera.PreviewCallback
来获取预览数据。
在onPreviewFrame
方法中,可以执行OCR识别