我正在尝试开发一个用于实时检测文本的应用程序,但遇到了一些问题...我在互联网上找到了一些示例,但一个示例却可以按我的要求调用摄像头实时检测。而且我需要尽可能的简单,因为我正在尝试为有视觉障碍的人制作一个应用程序,所以该应用程序无法访问相机,你知道吗?
无论如何,我尝试将用SurfaceView设置的相机与代码结合起来,以检测发现的文字,而当我打开应用程序时,它不会检测文字,只会停止。
我的activity_main布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#ffffff" />
<Button
android:id="@+id/startBtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|bottom"
android:background="@android:color/transparent" />
</FrameLayout>
我的活动主要代码:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PictureCallback {
public static final int REQUEST_CODE = 100;
private SurfaceHolder surfaceHolder;
private Camera camera;
private SurfaceView surfaceView;
private TextView textView;
private static final int REQUEST_IMAGE_CAPTURE = 1;
Bitmap bitmap;
String fileName = "TUTORIALWING_" + System.currentTimeMillis() + ".png";
private String[] neededPermissions = new String[]{CAMERA, WRITE_EXTERNAL_STORAGE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
if (surfaceView != null) {
boolean result = checkPermission();
if (result) {
setupSurfaceHolder();
}
}
textView = (TextView)findViewById(R.id.textView);
}
private boolean checkPermission() {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
ArrayList<String> permissionsNotGranted = new ArrayList<>();
for (String permission : neededPermissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsNotGranted.add(permission);
}
}
if (permissionsNotGranted.size() > 0) {
boolean shouldShowAlert = false;
for (String permission : permissionsNotGranted) {
shouldShowAlert = ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
}
if (shouldShowAlert) {
showPermissionAlert(permissionsNotGranted.toArray(new String[permissionsNotGranted.size()]));
} else {
requestPermissions(permissionsNotGranted.toArray(new String[permissionsNotGranted.size()]));
}
return false;
}
}
return true;
}
private void showPermissionAlert(final String[] permissions) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("permission required");
alertBuilder.setMessage("msg permissao");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
requestPermissions(permissions);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
}
private void requestPermissions(String[] permissions) {
ActivityCompat.requestPermissions(MainActivity.this, permissions, REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE:
for (int result : grantResults) {
if (result == PackageManager.PERMISSION_DENIED) {
// Not all permissions granted. Show message to the user.
return;
}
}
// All permissions are granted. So, do the appropriate work now.
break;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
private void setViewVisibility(int id, int visibility) {
View view = findViewById(id);
if (view != null) {
view.setVisibility(visibility);
}
}
private void setupSurfaceHolder() {
setViewVisibility(R.id.startBtn, View.VISIBLE);
setViewVisibility(R.id.surfaceView, View.VISIBLE);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
setBtnClick();
}
private void setBtnClick() {
Button startBtn = findViewById(R.id.startBtn);
if (startBtn != null) {
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
captureImage();
//detectText();
}
});
}
}
private void detectText() {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
FirebaseVisionTextDetector detector = FirebaseVision.getInstance().getVisionTextDetector();
detector.detectInImage(image).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
@Override
public void onSuccess(FirebaseVisionText firebaseVisionText) {
processText(firebaseVisionText);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
private void processText(FirebaseVisionText firebaseVisionText) {
List<FirebaseVisionText.Block> blocks = firebaseVisionText.getBlocks();
if(blocks == null || blocks.size() == 0) {
Toast.makeText(this, "No Text Found", Toast.LENGTH_LONG).show();
return;
}
for(FirebaseVisionText.Block block : blocks) {
String text = block.getText();
textView.setTextSize(24);
textView.setText(text);
}
}
public void captureImage() {
if (camera != null) {
camera.takePicture(null, null, this);
detectText();
}
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
startCamera();
}
private void startCamera() {
camera = Camera.open();
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
resetCamera();
}
private void resetCamera() {
if (surfaceHolder.getSurface() == null) {
// Return if preview surface does not exist
return;
}
if (camera != null) {
// Stop if preview surface is already running.
camera.stopPreview();
try {
// Set preview display
camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
// Start the camera preview...
camera.startPreview();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
private void releaseCamera() {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
saveImage(bytes);
resetCamera();
//Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
// }
// Intent intent = new Intent(MainActivity.this, LerActivity.class);
//startActivity(intent);
}
private void saveImage(byte[] bytes) {
FileOutputStream outStream;
//String fileName = "TUTORIALWING_" + System.currentTimeMillis() + ".jpg";
try {
// String fileName = "TUTORIALWING_" + System.currentTimeMillis() + ".png";
File file = new File(Environment.getExternalStorageDirectory(), fileName);
outStream = new FileOutputStream(file);
outStream.write(bytes);
bitmap.compress(Bitmap.CompressFormat.PNG,100, outStream);
outStream.close();
bitmap.recycle();
Toast.makeText(MainActivity.this, "Picture Saved: " + fileName, Toast.LENGTH_LONG).show();
//Intent intent = new Intent(this, LerActivity.class);
// intent.putExtra("imagem",fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/* Intent intent = new Intent(MainActivity.this, LerActivity.class);
Bitmap bitmap = null;
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs);
intent.putExtra("byteArray", bs.toByteArray());*/
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
//imageView.setImageBitmap(imageBitmap);
}
/* if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
Bitmap bitmap = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"),
0,getIntent().getByteArrayExtra("byteArray").length);
surfaceView.setImageBitmap(bitmap);
//bitmap.setImageBitmap(bitmap);
}*/
}
}