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

该博客探讨了如何使用ZXing库从可能模糊、脏污或刮擦的条形码图像中读取数据。作者首先将图像转换为BufferedImage,然后进行裁剪并保存为Bitmap。在尝试解码之前,对图像进行了预处理,包括转换矩阵和保存裁剪后的图像。尽管解码过程中可能遇到NotFoundException,但整个过程提供了测试条形码读取能力的详细步骤。

我正在使用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;

}

}

在 Android Studio 中,`java.awt.image.BufferedImage` 这类属于 Java AWT(Abstract Window Toolkit)的类不能直接使用,因为 AWT 是为桌面应用设计的,而 Android 有自己独立的图形和图像处理库。不过,可以使用 Android 提供的 `Bitmap` 类来替代 `BufferedImage` 实现类似的功能。 以下是在 Android 中使用 `Bitmap` 进行图像处理的示例代码: ```java import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { // 从文件中加载图片 File file = new File("/sdcard/1.png"); Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); // 这里可以对 bitmap 进行处理 // 将处理后的 bitmap 保存到文件 File outputFile = new File("/sdcard/2.png"); FileOutputStream fos = new FileOutputStream(outputFile); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述代码中,使用 `BitmapFactory.decodeFile` 方法从文件中加载图片,得到 `Bitmap` 对象,之后可以对 `Bitmap` 进行各种处理,最后使用 `compress` 方法将处理后的 `Bitmap` 保存到文件。 需要注意的是,在 Android 中访问外部存储需要在 `AndroidManifest.xml` 中添加相应的权限: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 并且在 Android 6.0(API 级别 23)及以上版本,还需要在运行时请求这些权限。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值