Java 用zxing从camera2拍摄的照片中解码数据矩阵
Java 用zxing从camera2拍摄的照片中解码数据矩阵,java,android,zxing,decoding,datamatrix,Java,Android,Zxing,Decoding,Datamatrix,经过10个多小时的搜索和尝试,我终于决定在这里提问。我正在使用android.hardware.camera2库从设备摄像头获取图像。现在,我想自动处理位图,并解码一个datamatrix代码,如果有任何关于zxing库的图片。有一个计时器每秒处理图像五次,一切正常,但它无法识别任何datamatrix代码。到目前为止,我有以下代码: public String readDataMatrix(Bitmap bitmap) { int width = bitmap.getWidth
经过10个多小时的搜索和尝试,我终于决定在这里提问。我正在使用android.hardware.camera2库从设备摄像头获取图像。现在,我想自动处理位图,并解码一个datamatrix代码,如果有任何关于zxing库的图片。有一个计时器每秒处理图像五次,一切正常,但它无法识别任何datamatrix代码。到目前为止,我有以下代码:
public String readDataMatrix(Bitmap bitmap) {
int width = bitmap.getWidth(),
height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
DataMatrixReader reader = new DataMatrixReader();
Result rawResult = null;
try {
rawResult = reader.decode(bBitmap);
String result = reader.decode(bBitmap).getText();
return result;
} catch (NotFoundException | ChecksumException | FormatException e) {
e.printStackTrace();
}
if (rawResult != null) {
Log.i(TAG, "==============================================");
Log.i(TAG, rawResult.getText());
Log.i(TAG, "==============================================");
}
return rawResult != null ? rawResult.getText() : null;
}
当使用QRCodeReader
替换DataMatrixReader
并使用qr码或使用multiformatrader
尝试时,这甚至不起作用我尝试处理的每一张图像都被zxing条形码扫描仪应用程序正确解码,因此问题在于代码。
如果有人能告诉我这是怎么回事,我会非常高兴,因为我相信在这之后,我是创造性地诅咒java的世界冠军 本尼
注:我在关于zxing的每一个线程中尝试了每一个解决方案,所以这真的是我最后的选择
public String readDataMatrix(Bitmap bitmap) {
int width = bitmap.getWidth();
height = bitmap.getHeight();
byte[] data = bitmap.getRowBytes();
Result rawResult = null;
Log.e("C2", data.length + " (" + width + "x" + height + ")");
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = mQrReader.decode(bitmap);
onQRCodeRead(rawResult.getText());
} catch (ReaderException ignored) {
/* Ignored */
} finally {
mQrReader.reset();
}
Result rawResult = null;
if (rawResult != null) {
Log.i(TAG, "==============================================");
Log.i(TAG, rawResult.getText());
Log.i(TAG, "==============================================");
}
return rawResult != null ? rawResult.getText() : null;
}
这对我来说很有效,使用自定义平面UV亮度源:
final public class PlanarYUVLuminanceSource extends LuminanceSource {
private final byte[] mYuvData;
public PlanarYUVLuminanceSource(byte[] yuvData, int width, int height) {
super(width, height);
mYuvData = yuvData;
}
@Override
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
final int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
final int offset = y * width;
System.arraycopy(mYuvData, offset, row, 0, width);
return row;
}
@Override
public byte[] getMatrix() {
return mYuvData;
}
@Override
public boolean isCropSupported() {
return true;
}
}
最终公共类计划UVluminanceSource扩展了LuminanceSource{
私有最终字节[]mYuvData;
公共平面UV亮度源(字节[]yuvData,整数宽度,整数高度){
超级(宽度、高度);
mYuvData=yuvData;
}
@凌驾
公共字节[]getRow(整数y,字节[]行){
如果(y<0 | | y>=getHeight()){
抛出新的IllegalArgumentException(“请求的行在图像外部:“+y”);
}
最终整数宽度=getWidth();
if(row==null | | row.length