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

使用Firebase ML Kit文字识别API遇报错,转图片格式后仍异常

Firebase ML Kit OCR 崩溃问题排查与修复

Let's tackle the two crash scenarios you're facing with Firebase ML Kit Text Recognition, and walk through actionable fixes for each.

First Crash (JPG Format)

Your initial error shows ML Kit failing to decode pixel data from the JPG:

A/native: image_util_common.cc:2757 Check failed: 'out_pix' Must be non NULL terminating.
A/native: image_util_common.cc:2797 Check failed: 'out_pix' Must be non NULL
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 19193 (FirebaseMLHandl)

This happens when ML Kit can't properly parse the image file—either the JPG is corrupted, the file path/Uri is invalid, or the built-in decoder hits an edge case with your image's encoding.

Second Crash (BMP Format)

After switching to BMP, the issue shifts to failed model loading, which leads to a segmentation fault:

E/zygote: The String#value field is not present on Android versions >= 6.0
E/native: jni_helper.cc:170 GetContents failed: /data/user/0/com.google.android.gms/app_vision/ocr/data/models/rpn_lstm_engine_tfmini.bincfg
E/native: jni_helper.cc:170 GetContents failed: /data/user/0/com.google.android.gms/app_vision/ocr/data/models/semanticlift_engine_0.2.bincfg
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 13092 (ocr_seg_4/13092)

This means the OCR models required by Google Play Services didn't download correctly or can't be accessed, leading to a null pointer crash when the engine tries to use them.


Actionable Fixes

1. Fix Image Loading (Bypass File Format Issues)

Instead of using FirebaseVisionImage.fromFilePath(), load the image into a Bitmap first—this standardizes the image data and avoids ML Kit's direct file decoding quirks:

// Replace your existing image initialization code with this
File file = // your image file
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

// Verify the bitmap loaded successfully
if (bitmap == null) {
    Log.e(TAG, "Failed to load image file - check if the file exists and is not corrupted");
    return;
}

FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);

2. Fix OCR Model Loading Failures

  • Update Google Play Services: ML Kit OCR relies on GMS to provide model files. Make sure the device has the latest version of Google Play Services (go to Settings > Apps > Google Play Services > Check for updates).
  • Clear GMS Cache: Corrupted model downloads are a common culprit. Go to Settings > Apps > Google Play Services > Storage > Clear Cache, then restart your app—this triggers a fresh download of the OCR models.
  • Verify Storage Permissions: If your image is stored in external storage, ensure your app has the READ_EXTERNAL_STORAGE permission (and the user has granted it):
    // Check and request permission if needed
    if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions((Activity) context,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                1001); // Use your own request code
    }
    

3. Upgrade to the Latest ML Kit OCR API

The old VisionTextDetector is deprecated—switch to the newer on-device text recognizer, which is more stable and maintained:

// Initialize the v2 recognizer
FirebaseVisionTextRecognizer recognizer = FirebaseVision.getInstance()
        .getOnDeviceTextRecognizer();

// Process the image (use the Bitmap-based image we created earlier)
Task<FirebaseVisionText> result = recognizer.processImage(image)
        .addOnSuccessListener(firebaseVisionText -> {
            List<String> words = new ArrayList<>();
            for (FirebaseVisionText.Block block : firebaseVisionText.getBlocks()) {
                Log.d(TAG, "Detected Text: " + block.getText());
                for (FirebaseVisionText.Line line : block.getLines()) {
                    for (FirebaseVisionText.Element element : line.getElements()) {
                        String textPart = element.getText();
                        words.add(textPart);
                        Log.d(TAG, "textPart: " + textPart);
                    }
                }
            }
        })
        .addOnFailureListener(e -> {
            Log.e(TAG, "OCR failed", e);
        });

Make sure your app's build.gradle has the latest dependencies:

implementation 'com.google.firebase:firebase-ml-vision:24.1.0'
implementation 'com.google.firebase:firebase-ml-vision-text-recognition-model:20.0.0'

4. Clean Up Resources

Always close the recognizer/detector when you're done with it to avoid resource leaks:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (recognizer != null) {
        recognizer.close();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:20:20