这两天又在原始项目中使用opencv 加入了一些图像处理的方法,顺便改善了一下代码质量,示例App已经更新。
引入对应依赖
可进入官网选择对应语言依赖,我使用的是英文
implementation 'com.google.mlkit:text-recognition:16.0.0'
然后我就直接放代码了,比较简单就一个页面,关注按钮事件,顺腾摸瓜找下去就行了。这里提供一个示例Apk,方便查看效果。
这里发现apk文件复制不进来,那算了,有需要可以私发。
apk已经链接到顶部了, 哈哈。
页面代码
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA_PERMISSION = 333;
private static final int REQUEST_IMAGE_CAPTURE = 666;
private TextView tv_result;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_result=findViewById(R.id.tv_result);
image=findViewById(R.id.image);
//权限检查
requestCameraPermission();
Button btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dispatchTakePictureIntent();//调本地相机
}
});
Button btnNiDie=findViewById(R.id.btn_dieDie);
btnNiDie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.six);
recognizeTextFromBitmap(bmp);
}
});
Button btnNiYeYe=findViewById(R.id.btn_yeYe);
btnNiYeYe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openSysAlbum();
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
private void requestCameraPermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
image.setImageBitmap(imageBitmap);
// 在这里使用imageBitmap进行文字识别
recognizeTextFromBitmap(imageBitmap);
} else if (requestCode == SELECT_IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
Uri uri = data.getData();
Bitmap bitmap111 = getBitmapFromUri(uri, this);
recognizeTextFromBitmap(bitmap111);
}
}
private void recognizeTextFromBitmap(Bitmap bitmap) {
InputImage image = InputImage.fromBitmap(bitmap, 0);
TextRecognizer myRecognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
myRecognizer.process(image)
.addOnSuccessListener(new OnSuccessListener<Text>() {
@Override
public void onSuccess(com.google.mlkit.vision.text.Text text) {
String text1 = text.getText();
tv_result.setText(text1);
myRecognizer.close();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// 处理错误
e.printStackTrace();
// 释放资源
myRecognizer.close();
}
});
}
private final int SELECT_IMAGE_REQUEST_CODE = 999;
/**
* 打开系统相册
*/
private void openSysAlbum() {
Intent innerIntent = new Intent();
if (Build.VERSION.SDK_INT < 19) {
innerIntent.setAction(Intent.ACTION_GET_CONTENT);
} else {
innerIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
innerIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
Intent wrapperIntent = Intent.createChooser(innerIntent, "");
startActivityForResult(wrapperIntent, SELECT_IMAGE_REQUEST_CODE);
}
public Bitmap getBitmapFromUri(Uri uri, Context context) {
ContentResolver contentResolver = context.getContentResolver();
InputStream inputStream = null;
try {
inputStream = contentResolver.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn"
android:text="拍照"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_yeYe"
android:text="导入"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<Button
android:id="@+id/btn_dieDie"
android:text="读取"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>