java image bitmap_java - 将BinaryBitmap映射到BufferedImage - SO中文参考 - www.soinside.com

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

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9TQVVwdy5wbmcifQ==

理想情况下,条形码始终放置在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 hint = new Hashtable();

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值