使用 chaquopy 时,应用程序在设备中崩溃但在模拟器中不崩溃

app crashes in device but not in emulator while using chaquopy

我在 android 工作室中使用 chaquopy 创建了人脸检测。当我 运行 应用程序进入模拟器时,它工作正常。但是当我启动 phone 时,它崩溃了。在 运行 中显示

I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
I/Toast: Show toast from OpPackageName:com.example.chaquopy, PackageName:com.example.chaquopy
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@57230c5
I/Adreno: BLTLIB: Semaphore Timeout, disabling MT
I/Adreno: BLTLIB: Semaphore Timeout count 1
I/Adreno: BLTLIB: Semaphore Timeout count 2
I/Adreno: BLTLIB: Semaphore Timeout count 3
I/Adreno: BLTLIB: Semaphore Timeout count 4
I/Adreno: BLTLIB: Semaphore Timeout count 5
I/Adreno: BLTLIB: Semaphore Timeout count 6
I/Adreno: BLTLIB: Semaphore Timeout count 7
I/art: Waiting for a blocking GC Alloc
I/art: Starting a blocking GC Alloc
    Starting a blocking GC Alloc
I/art: Alloc partial concurrent mark sweep GC freed 220(9KB) AllocSpace objects, 1(480KB) LOS objects, 8% free, 171MB/187MB, paused 262us total 14.558ms
    Starting a blocking GC Alloc
I/art: Starting a blocking GC Alloc
I/art: Alloc concurrent mark sweep GC freed 27(752B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 237us total 25.550ms
    Forcing collection of SoftReferences for 35MB allocation
    Starting a blocking GC Alloc
I/art: Alloc concurrent mark sweep GC freed 2(48B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 230us total 24.816ms
W/art: Throwing OutOfMemoryError "Failed to allocate a 36742016 byte allocation with 16777216 free bytes and 20MB until OOM"
I/art: Starting a blocking GC Alloc
    Starting a blocking GC Alloc
I/art: Starting a blocking GC Alloc
I/art: Alloc partial concurrent mark sweep GC freed 4(96B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 430us total 22.384ms
    Starting a blocking GC Alloc
I/art: Alloc concurrent mark sweep GC freed 2(48B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 333us total 35.743ms
    Forcing collection of SoftReferences for 35MB allocation
    Starting a blocking GC Alloc
I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 249us total 28.176ms
    Starting a blocking GC HomogeneousSpaceCompact
I/art: HomogeneousSpaceCompact marksweep + semispace GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 29.551ms total 29.551ms
W/art: Throwing OutOfMemoryError "Failed to allocate a 36742016 byte allocation with 16777216 free bytes and 20MB until OOM"
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chaquopy, PID: 32703
    java.lang.OutOfMemoryError: Failed to allocate a 36742016 byte allocation with 16777216 free bytes and 20MB until OOM
        at java.lang.StringFactory.newStringFromChars(StringFactory.java:218)
        at java.lang.StringFactory.newStringFromBytes(StringFactory.java:203)
        at java.lang.StringFactory.newStringFromBytes(StringFactory.java:63)
        at android.util.Base64.encodeToString(Base64.java:456)
        at com.example.chaquopy.MainActivity.getStringImage(MainActivity.java:102)
        at com.example.chaquopy.MainActivity.access[=10=]0(MainActivity.java:31)
        at com.example.chaquopy.MainActivity.onClick(MainActivity.java:76)
        at android.view.View.performClick(View.java:5619)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View$PerformClick.run(View.java:22298)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
W/AndroidRuntime: finished raiseRlimit, rlim_cur:4096  rlim_max:4096

我的密码是

 Submitbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Submit Button is clicked",Toast.LENGTH_SHORT).show();
                //by clicking get image from image view
                bitmapDrawable = (BitmapDrawable) selectedImage.getDrawable();
                bitmap = bitmapDrawable.getBitmap();
                imageString = getStringImage(bitmap);
                //in imageString we get encoded image
                //passing the string into python script
                PyObject pyObject = py.getModule("scripts");
                PyObject obj = pyObject.callAttr("main", imageString);
                //converted to string
                String string = obj.toString();
                //converted to byte array
                byte data[] = android.util.Base64.decode(string,0);
                //converted to bitmap
                Bitmap returnBitmap = BitmapFactory.decodeByteArray(data,0,data.length);

                //now set bitmap to image view
                selectedImage.setImageBitmap(returnBitmap);

            }
        });

private String getStringImage(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);
        //storing in byte array
        byte[] imageBytes = byteArrayOutputStream.toByteArray();
        //encoding string
        String encodedImage = android.util.Base64.encodeToString(imageBytes, 0);
        return encodedImage;
    }

在我的代码中,当用户单击 submitbtn 时,来自 imageview 的图像将编码为字符串并发送到 python 模块。然后在 python 脚本中进行人脸检测后再次 return 字符串,然后在 main 中我完成剩下的工作。 我能做什么?

这里不用Base64。只需将 byte[] 数组直接传递给 Python,然后使用 this FAQ.

中显示的方法之一在 Python 中接收它

同样,当 return 从 Python 获取图像时,您应该 return 一个 bytes 对象,并使用 byte[] 将其转换为数组 toJava,如 chaquopy-matplotlib 示例所示。

此外,如果您的 PNG 图片为 36 MB,那么我认为它是高分辨率照片,因此您可能应该改用 JPG 格式。