Android与内存位图不兼容

我正在创建一个从相机获取图像的应用程序(使用CameraKit库),处理图像并使用Google Vision Api进行OCR读取,并获取此错误:

致命例外:主要过程:com。 ,PID:1938 java.lang.OutOfMemoryError:在android.graphics.Bitmap.nativeCreate(Native Method)处的dalvik.system.VMRuntime.newNonMovableArray(Native Method)时,未能分配63701004字节分配,其空闲字节数为16777216,在android.graphics.Bitmap.createBitmap(Bitmap.java:849)上的android.graphics.Bitmap.createBitmap(Bitmap.java:882)在android.graphics.Bitmap.createBitmap(Bitmap.java:905) *。****。Reader.ReaderResultActivity.createContrast(ReaderResultActivity.java:123)at com. *****。****。Reader.ReaderResultActivity.onCreate(ReaderResultActivity.java:47)at android.app。 Activity.performCreate(Activity.java:6672)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)at android.app.ActivityThread.handleLaunchActivity(ActivityThread) .java:2724)at android.app.ActivityThread.-wrap12(ActivityThread.java)at android.app.ActivityThread $ H.handleMessage(ActivityTh read.java:1473)在android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main(ActivityThread.java:6123) )at com.android.internal.os.ZygoteInit上的java.lang.reflect.Method.invoke(Native方法)$ com.android.internal.os.ZygoteInit.main上的$ MethodAndArgsCaller.run(ZygoteInit.java:867)(ZygoteInit的.java:757)

ReaderResultActivity代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reader_result);

    ImageView img1 = (ImageView)findViewById(R.id.imageView2);
    ImageView img2 = (ImageView)findViewById(R.id.imageView3);
    ImageView img3 = (ImageView)findViewById(R.id.imageView4);
    TextView scanResults = (TextView)findViewById(R.id.textView);

    //Get bitmap from a static class.
    Bitmap bitmap = Reader.img;

    Bitmap grayScale = toGrayscale(bitmap);
    Bitmap blackWhiteImage = createContrast(grayScale, 50);
    Bitmap invertColor = invertColor(blackWhiteImage);

    //Show process steps
    img1.setImageBitmap(grayScale);
    img2.setImageBitmap(blackWhiteImage);
    img3.setImageBitmap(invertColor);

    TextRecognizer detector = new TextRecognizer.Builder(getApplicationContext()).build();

    try {

        if (detector.isOperational()) {
            Frame frame = new Frame.Builder().setBitmap(invertColor).build();
            SparseArray<TextBlock> textBlocks = detector.detect(frame);
            String blocks = "";
            String lines = "";
            String words = "";
            for (int index = 0; index < textBlocks.size(); index++) {
                //extract scanned text blocks here
                TextBlock tBlock = textBlocks.valueAt(index);
                blocks = blocks + tBlock.getValue() + "n" + "n";
                for (Text line : tBlock.getComponents()) {
                    //extract scanned text lines here
                    lines = lines + line.getValue() + "n";
                    for (Text element : line.getComponents()) {
                        //extract scanned text words here
                        words = words + element.getValue() + ", ";
                    }
                }
            }
            if (textBlocks.size() == 0) {
                scanResults.setText("Scan Failed: Found nothing to scan");
            } else {
                lines = lines.replaceAll("o", "0");
                lines = lines.replaceAll("A", "1");

                scanResults.setText(lines + "n");
            }
        } else {
            scanResults.setText("Could not set up the detector!");
        }
    } catch (Exception e) {
        Toast.makeText(this, "Failed to load Image", Toast.LENGTH_SHORT)
                .show();
        Log.e("312", e.toString());
    }
}

private Bitmap processImage(Bitmap bitmap){
    Bitmap grayScale = toGrayscale(bitmap);
    Bitmap blackWhiteImage = createContrast(grayScale, 50);
    Bitmap invertColor = invertColor(blackWhiteImage);

    return invertColor;
}
public Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, bmpOriginal.getConfig());
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

public static Bitmap createContrast(Bitmap src, double value) {
    // image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // color information
    int A, R, G, B;
    int pixel;
    // get contrast value
    double contrast = Math.pow((100 + value) / 100, 2);

    // scan through all pixels
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get pixel color
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            // apply filter contrast for every channel R, G, B
            R = Color.red(pixel);
            R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(R < 0) { R = 0; }
            else if(R > 255) { R = 255; }

            G = Color.red(pixel);
            G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(G < 0) { G = 0; }
            else if(G > 255) { G = 255; }

            B = Color.red(pixel);
            B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(B < 0) { B = 0; }
            else if(B > 255) { B = 255; }

            // set new pixel color to output bitmap
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

    return bmOut;
}

Bitmap invertColor(Bitmap src){
    Bitmap copy = src.copy(src.getConfig(), true);

    for (int x = 0; x < copy.getWidth(); ++x) {
        for (int y = 0; y < copy.getHeight(); ++y) {
            int color = copy.getPixel(x, y);
            int r = Color.red(color);
            int g = Color.green(color);
            int b = Color.blue(color);
            int avg = (r + g + b) / 3;
            int newColor = Color.argb(255, 255 - avg, 255 - avg, 255 - avg);
            copy.setPixel(x, y, newColor);
        }
    }


    return copy;
}

已经尝试在Manifest中做到这一点

机器人:largeHeap = “真”

但是,应用程序停止运行时:

ReaderResultActivity.createContrast(ReaderResultActivity.java:123)

没有“largeHeap”标签时出现错误的同一行。 只是不知道该怎么做,但我认为这与每个进程函数中的所有“Bitmap.CreateBitmap”都有关系。 但是如果没有这样做,在OCR读取中,会出现错误,指出位图格式不正确。


您在不同的图像视图中加载三个位图,而根据您要在UI上显示的大小进行缩放。

Android设备的摄像头以比设备屏幕密度高得多的分辨率捕捉图片。

鉴于您正在使用有限的内存,理想情况下,您只需要在内存中加载较低分辨率的版本。 较低分辨率的版本应该与显示它的UI组件的大小相匹配。 分辨率更高的图像不会提供任何可见的好处,但仍会占用宝贵的内存,并由于其他即时缩放而导致额外的性能开销。

您可以按照开发者文档建议对其进行优化 - https://developer.android.com/topic/performance/graphics/load-bitmap.html

链接地址: http://www.djcxy.com/p/33663.html

上一篇: Android Out of Memory with bitmap

下一篇: Unable to convert viewgroup into bitmap in Android