Reputation: 2154
I am processing an Image to make it Tesseract-OCR friendly using Catalano framework. Thresholding is working fine but when I try to UnsharpMasking or resizing I get an error Couldn't correct orientation: java.lang.IllegalStateException: Can't call setPixels() on a recycled bitmap even though I have set BitmapFactory options inmutable to true & minsdk=11.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeFile(ImagePath, options);
Bitmap bitmap = BitmapFactory.decodeFile(ImagePath, options);
FastBitmap fb = new FastBitmap(bitmap);
Grayscale grayScale = new Grayscale();
grayScale.applyInPlace(fb);
switch (thresholdFilterName) {
case "Threshold": Threshold t = new Threshold(100);
t.applyInPlace(fb);
break;
case "BradleyLocalThreshold": BradleyLocalThreshold blt = new BradleyLocalThreshold();
blt.applyInPlace(fb);
break;
case "SauvolaThreshold": SauvolaThreshold st =new SauvolaThreshold();
st.applyInPlace(fb);
break;
case "NiblackThreshold": NiblackThreshold nt =new NiblackThreshold();
nt.applyInPlace(fb);
break;
case "WolfJoulionThreshold": WolfJoulionThreshold wjt =new WolfJoulionThreshold();
wjt.applyInPlace(fb);
break;
default: WolfJoulionThreshold wjtD =new WolfJoulionThreshold();
wjtD.applyInPlace(fb);
break;
}
UnsharpMasking um= new UnsharpMasking();
um.applyInPlace(fb);
// exception is thrown here.
Sharpen s = new Sharpen();
s.applyInPlace(fb);
/*int newWidth=1191;
int newHeight=2000;
Resize rb = new Resize(newWidth, newHeight);
// and exception is thrown here also if i comment the above unsharpmasking.
// ResizeBicubic rb =new ResizeBicubic(newWidth, newHeight);
// ResizeBilinear rb = new ResizeBilinear(newWidth, newHeight);
// ResizeNearestNeighbor rb = new ResizeNearestNeighbor(newWidth, newHeight);
rb.applyInPlace(fb);*/
bitmap = fb.toBitmap();
processedImage.setImageBitmap(bitmap);
I have read for pre-processing images for OCR ImageMagick is also pretty good but I haven't found anything concrete for how to use it for the OCR purpose in android all I got is command line tool. Also I have reffered https://github.com/paulasiimwe/Android-ImageMagick but that also didnt help much
Upvotes: 0
Views: 957
Reputation: 5836
Looks like you found a bug in the Catalano Framework code. Consider filing a bug report on the project Issues page.
In the Catalano Framework's UnsharpMasking
class in its applyInPlace
method, recycle()
is called on the bitmap, causing the next operation you perform on that bitmap to fail.
A workaround would be to remove the blur.recycle()
statement from the Catalano Framework code.
As an alternative workaround, if you don't have access to the Catalano Framework code because you're using it as a JAR, you could subclass the UnsharpMasking
class yourself in a new class and override the applyInPlace
method to remove the recycle()
statement. Then you would refer to the subclassed version from your alternative version of the code as follows:
//UnsharpMasking um = new UnsharpMasking();
MySafeUnsharpMasking um = new MySafeUnsharpMasking();
um.applyInPlace(fb);
Upvotes: 1