Scanner icon indicating copy to clipboard operation
Scanner copied to clipboard

自定义scanner中使用nv21ToBitmap无效

Open jarvis4901 opened this issue 3 years ago • 4 comments

` private IScanner buildScaner() { return new IScanner() { /** * 这里实现自己的识别器,并把识别结果返回 * * @param data 矩形框内nv21图像数据 * @param width 图像宽度 * @param height 图像高度 * @return * @throws Exception */ @Override public Result scan(byte[] data, int width, int height) throws Exception { Result result = new Result();

            if (isDataCopied()) {
                // 如果你想转为Bitmap,请使用NV21.nv21ToBitmap(byte[] nv21, int width, int height)
                NV21 nv21 = new NV21(mContext);
                Bitmap bitmap = nv21.nv21ToBitmap(data, width, height);   //这里的bitmap为空
                try {
                    tessBaseAPI.setImage(bitmap);
                    String text = tessBaseAPI.getUTF8Text();
                    if (text != null) {
                        Log.i("tess result:", text);
                        result.data = text;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;

        }
    };
}

`

微信截图_20210309101547 微信截图_20210309101606

nv21.nv21ToBitmap方法返回一直为空

jarvis4901 avatar Mar 09 '21 08:03 jarvis4901

调试下,看下报什么错,正常情况要不抛异常,要不有返回值,不会返回null,还有android版本多少

shouzhong avatar Mar 10 '21 02:03 shouzhong

没返回值时return null,不要有返回值

shouzhong avatar Mar 10 '21 02:03 shouzhong

而且如果你是竖屏扫描,返回的图像是横着的,如果你识别器不支持横图像识别,建议setRotateDegree90Recognition(true)

shouzhong avatar Mar 10 '21 02:03 shouzhong

如果这个方法不行的话,尝试使用以下方法: Bitmap nv21ToBitmap(byte[] nv21, int width, int height) { Bitmap bitmap = null; try { YuvImage image = new YuvImage(nv21, ImageFormat.NV21, width, height, null); ByteArrayOutputStream stream = new ByteArrayOutputStream(); image.compressToJpeg(new Rect(0, 0, width, height), 100, stream); bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size()); stream.close(); } catch (Exception e) { e.printStackTrace(); } return bitmap; }

shouzhong avatar Mar 10 '21 03:03 shouzhong