将BinaryBitmap映射到BufferedImage

问题描述 投票:1回答:1

我正在使用zxing从这样的扫描图像中读取条形码:

enter image description here

理想情况下,条形码始终放置在2/5的位置,但有时条形码会模糊,弄脏或刮擦,出于测试目的,需要保存发送到读取器的位图,基于此答案:Convert byte array of data type TYPE_4BYTE_ABGR to BufferedImage我正在尝试保存croppedBitmap失败,真的很感谢任何帮助。

private static String decodeFile(File file) throws IOException, NotFoundException {
    BufferedImage bufferedImage = ImageIO.read(file);
    LuminanceSource source = new 
    BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    System.out.println(bitmap.getWidth() + " x " + bitmap.getHeight());

    int width = bitmap.getWidth();
    int height = bitmap.getHeight() / 5;
    int top = height;
    BinaryBitmap croppedBitmap = bitmap.crop(0, top, width, height);

    int[] dst = new int[width * height];

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int a = croppedBitmap.getBlackMatrix().get(i, j) ? 1 & 0xff : 0;
            int b = croppedBitmap.getBlackMatrix().get(i, j) ? 1 & 0xff : 0;
            int g = croppedBitmap.getBlackMatrix().get(i, j) ? 1 & 0xff : 0;
            int r = croppedBitmap.getBlackMatrix().get(i, j) ? 1 & 0xff : 0;
            dst[(i + 1) * j] = (a << 24) | (r << 16) | (g << 8) | b;
        }
    }

    BufferedImage image = new BufferedImage(width, height, 
    BufferedImage.TYPE_INT_ARGB);
    image.setRGB(0, 0, width, height, dst, 0, width);

    boolean r = ImageIO.write(image, "bmp", new File("croppedBitmap.bmp"));
    System.out.println("Write bmp:" + r);

    Hashtable<DecodeHintType, Object> hint = new Hashtable<DecodeHintType, Object>();
    hint.put(DecodeHintType.TRY_HARDER, BarcodeFormat.CODE_39);

    try {
        MultiFormatReader reader = new MultiFormatReader();
        Result result = reader.decode(bitmap, hint);
        return result.getText();
    } catch (NotFoundException e) {
        System.out.println("Decode failed.");
        return null;
    }
}
java zxing
1个回答
0
投票
使用toBufferedImage

toBufferedImage

© www.soinside.com 2019 - 2024. All rights reserved.