有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用firebasemlkit访问cloud vision api时关闭javasocket

我正在运行here和Firebase OCR文档示例的一个子集。使用Firebase ML Kit运行云文本识别时,我发现socket已关闭。在带有Android Pie的设备上运行此功能

我尝试过清除Google Play服务缓存,重新启动设备(使其工作1次),创建网络安全配置以允许从here发送/接收HTTP请求

从资源创建位图并将其传递到runCloudTextRecognition

Android清单

<uses-permission 安卓:name="安卓.permission.INTERNET" />
<application>
<meta-data
            安卓:name="com.google.firebase.ml.vision.DEPENDENCIES"
            安卓:value="ocr" />
</application>

runCloudTextRecognition和getBitmapFromAsset

public static Bitmap getBitmapFromAsset(Context context, String filePath) {
        AssetManager assetManager = context.getAssets();
        InputStream istr;
        Bitmap bitmap = null;
        try {
            istr = assetManager.open(filePath);
            bitmap = BitmapFactory.decodeStream(istr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

private void runCloudTextRecognition(Bitmap mSelectedImage) {
        //mCloudButton.setEnabled(false);
        FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(mSelectedImage);
        final FirebaseVisionTextRecognizer recognizer = FirebaseVision.getInstance()
                .getCloudTextRecognizer();
        recognizer.processImage(image)

                .addOnSuccessListener(
                        new OnSuccessListener<FirebaseVisionText>() {
                            @Override
                            public void onSuccess(FirebaseVisionText texts) {
                                //mCloudButton.setEnabled(true);
                                Log.e("CloudOCR_Success",texts.getText());
                            }
                        })
                .addOnFailureListener(
                        new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                // Task failed with an exception
                                //mCloudButton.setEnabled(true);
                                e.printStackTrace();
                                try {
                                    recognizer.close();
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                            }
                        });
    }

LogCat

E/ImageAnnotatorTask: batchAnnotateImages call failed with exception: 
    java.net.SocketTimeoutException: timeout
        at com.安卓.okhttp.okio.Okio$3.newTimeoutException(Okio.java:212)
        at com.安卓.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:261)
        at com.安卓.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
        at com.安卓.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
        at com.安卓.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
        at com.安卓.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
        at com.安卓.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
        at com.安卓.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
        at com.安卓.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
        at com.安卓.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
        at com.安卓.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
        at com.安卓.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
        at com.安卓.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
        at com.安卓.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
        at com.安卓.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:26)
        at com.google.安卓.gms.internal.firebase_ml.zzgd.<init>(Unknown Source:5)
        at com.google.安卓.gms.internal.firebase_ml.zzga.zzfg(Unknown Source:43)
        at com.google.安卓.gms.internal.firebase_ml.zzfk.zzfa(Unknown Source:131)
        at com.google.安卓.gms.internal.firebase_ml.zzer.zzei(Unknown Source:51)
        at com.google.安卓.gms.internal.firebase_ml.zzqf.zza(Unknown Source:68)
        at com.google.安卓.gms.internal.firebase_ml.zzqg.zza(Unknown Source:23)
        at com.google.安卓.gms.internal.firebase_ml.zznt.call(Unknown Source:4)
        at com.google.安卓.gms.internal.firebase_ml.zznn.zza(Unknown Source:30)
        at com.google.安卓.gms.internal.firebase_ml.zznq.run(Unknown Source:2)
        at 安卓.os.Handler.handleCallback(Handler.java:873)
        at 安卓.os.Handler.dispatchMessage(Handler.java:99)
        at com.google.安卓.gms.internal.firebase_ml.zzf.dispatchMessage(Unknown Source:6)
        at 安卓.os.Looper.loop(Looper.java:193)
        at 安卓.os.HandlerThread.run(HandlerThread.java:65)
     Caused by: java.net.SocketException: socket is closed
        at com.安卓.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:551)
        at com.安卓.okhttp.okio.Okio$2.read(Okio.java:136)
        at com.安卓.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
        at com.安卓.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306) 
        at com.安卓.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300) 
        at com.安卓.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196) 
        at com.安卓.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186) 
        at com.安卓.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127) 
        at com.安卓.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737) 
        at com.安卓.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609) 
        at com.安卓.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471) 
        at com.安卓.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407) 
        at com.安卓.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538) 
        at com.安卓.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) 
        at com.安卓.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:26) 
        at com.google.安卓.gms.internal.firebase_ml.zzgd.<init>(Unknown Source:5) 
        at com.google.安卓.gms.internal.firebase_ml.zzga.zzfg(Unknown Source:43) 
        at com.google.安卓.gms.internal.firebase_ml.zzfk.zzfa(Unknown Source:131) 
        at com.google.安卓.gms.internal.firebase_ml.zzer.zzei(Unknown Source:51) 
        at com.google.安卓.gms.internal.firebase_ml.zzqf.zza(Unknown Source:68) 
        at com.google.安卓.gms.internal.firebase_ml.zzqg.zza(Unknown Source:23) 
        at com.google.安卓.gms.internal.firebase_ml.zznt.call(Unknown Source:4) 
        at com.google.安卓.gms.internal.firebase_ml.zznn.zza(Unknown Source:30) 
        at com.google.安卓.gms.internal.firebase_ml.zznq.run(Unknown Source:2) 
        at 安卓.os.Handler.handleCallback(Handler.java:873) 
        at 安卓.os.Handler.dispatchMessage(Handler.java:99) 
        at com.google.安卓.gms.internal.firebase_ml.zzf.dispatchMessage(Unknown Source:6) 
        at 安卓.os.Looper.loop(Looper.java:193) 
        at 安卓.os.HandlerThread.run(HandlerThread.java:65) 

共 (1) 个答案

  1. # 1 楼答案

    我通过使用位图工厂Options将位图的大小减小到recommended大小来解决我的问题。有关处理大型位图的详细信息,请参见this,也可以使用类似Glide的库。 更新firebase库也有帮助