You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

华为手机裁剪图片后文字无法识别问题求助(Android Studio)

华为手机裁剪图片后文字识别失败的解决建议

我使用一款图片裁剪插件,在多款安卓手机上运行正常,但在华为手机中,裁剪图片里的指定文字后无法识别出文字,求解决建议。

我尝试的代码

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 103){ 
        if (resultCode == RESULT_OK){
            if (requestCode == IMAGE_PICK_CAMERA_CODE){

                CropImage.activity(image_uri).setGuidelines(CropImageView.Guidelines.ON).start(this);
            }
    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if(resultCode ==RESULT_OK) {
            Uri resultUri = result.getUri();
            resultUri.getPath();
            BitmapDrawable bitmapDrawable;

            ScannedImage.setImageURI(resultUri);
            bitmapDrawable = (BitmapDrawable) ScannedImage.getDrawable();

            Bitmap bitmap = bitmapDrawable.getBitmap();
            TextRecognizer recognizer = new TextRecognizer.Builder(getApplicationContext()).build();

            Frame frame = new Frame.Builder().setBitmap(bitmap).build();
            SparseArray<TextBlock> items = recognizer.detect(frame);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < items.size(); i++) {
                TextBlock myItem = items.valueAt(i);
                sb.append(myItem.getValue());
                sb.append("\n");
            }

            String sTextFromET = sb.toString().replaceAll("\\s+", "");
            edt_card_number_inputted1.setText(sTextFromET);

        }}}}

解决建议

  • 优化Bitmap获取方式:华为手机的ImageView可能会自动压缩图片,导致文字识别的清晰度不足。建议直接从Uri读取Bitmap,避免通过ImageView的Drawable获取:
InputStream inputStream = getContentResolver().openInputStream(resultUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888; // 使用高质量色彩配置
options.inSampleSize = 1; // 不缩放图片
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
  • 修正图片方向:华为设备的相机或裁剪插件可能导致图片方向异常,文字识别依赖正确的图片方向。可以读取Exif信息调整Bitmap方向:
// 读取Exif信息
ExifInterface exif = new ExifInterface(getContentResolver().openInputStream(resultUri));
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
// 创建旋转矩阵
Matrix matrix = new Matrix();
switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        matrix.postRotate(90);
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        matrix.postRotate(180);
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        matrix.postRotate(270);
        break;
}
// 调整Bitmap方向
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  • 检查权限与上下文:华为的权限管控更严格,确保已动态申请READ_EXTERNAL_STORAGE权限后再执行识别逻辑。同时将TextRecognizer.Builder(getApplicationContext())替换为当前Activity的上下文,避免上下文兼容性问题:
TextRecognizer recognizer = new TextRecognizer.Builder(this).build();
  • 更换文字识别引擎:原生TextRecognizer在部分华为设备上兼容性较差,建议改用Google ML Kit文字识别,它对多品牌安卓设备的适配性更强,识别准确率也更高。

内容的提问来源于stack exchange,提问作者Shainnah Jane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 15:57:08