java条码大小_java - ML Kit条形码扫描:无效的图像数据大小

在Android应用中,使用camera2捕获图像并使用Firebase ML Kit进行条形码检测时遇到问题。错误提示为'无效的图像数据大小'。问题可能源于图像格式不正确,应避免使用NV21格式。建议直接使用FirebaseVisionImage.fromMediaImage()创建FirebaseVisionImage,或者在两个活动间转换为Bitmap以避免图像数据问题。
摘要由CSDN通过智能技术生成

我想在捕获的图像中检测条形码。我使用android的camera2捕获图像。此后,将检索图像的元数据并将图像保存到设备。元数据全部传递到下一个活动,该活动是应用程序尝试检测条形码的地方。

下一个活动是从先前保存的文件创建一个byte []。接下来,使用随意图传递的数据创建相关的FirebaseVision对象。最后,应用程序尝试调用detectInImage()方法,该方法会引发错误:

“java.lang.IllegalArgumentException:无效的图像数据大小。”

我怀疑这是因为捕获的图像太大,但是我似乎无法弄清楚如何捕获较小的图像,并且在参考文档中也找不到任何有关允许的最大尺寸的信息。非常感谢您提供有关此错误及其解决方法的信息。以下是我认为是相关的代码。private final ImageReader.OnImageAvailableListener onImageAvailableListener

= new ImageReader.OnImageAvailableListener() {

@Override

public void onImageAvailable(ImageReader imageReader) {

try{

// Semaphore ensures date is recorded before starting next activity

storeData.acquire();

Image resultImg = imageReader.acquireNextImage(); // Image from camera

imgWidth = resultImg.getWidth();

imgHeight = resultImg.getHeight();

ByteBuffer buffer = resultImg.getPlanes()[0].getBuffer();

data = new byte[buffer.remaining()]; // Byte array with the images data

buffer.get(data);

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

// Note: mediaFile directs to Pictures/"ThisProject" folder

File media = new File(mediaFile.getPath() +

File.separator + "IMG_" + timeStamp + ".jpg");

// Saving the image

FileOutputStream fos = null;

try {

fos = new FileOutputStream(media);

fos.write(data);

uri = Uri.fromFile(media);

} catch (IOException e) {

Log.e(TAG, e.getMessage());

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

Log.e(TAG, e.getMessage());

}

}

}

resultImg.close();

} catch (InterruptedException e) {

Log.e(TAG, e.getMessage());

}

storeData.release();

}

};

这实际上是检索图像的高度和宽度,然后将其写入文件。

发送到下一个活动的数据包括:图像宽度,图像高度,图像旋转和指向文件的Uri。

使用此工具,我尝试使用Firebase ML Kit检测条形码:

// uri is the uri referencing the saved image

File f = new File(uri.getPath());

data = new byte[(int) f.length()];

try{

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));

DataInputStream dis = new DataInputStream(bis);

dis.readFully(data);

} catch (IOException e) {

Log.e(TAG, e.getMessage());

}

FirebaseVisionBarcodeDetectorOptions options = new FirebaseVisionBarcodeDetectorOptions.Builder().setBarcodeFormats(

FirebaseVisionBarcode.FORMAT_QR_CODE,

FirebaseVisionBarcode.FORMAT_DATA_MATRIX

).build();

FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);

FirebaseVisionImage image;

int rotationResult;

switch (imgRotation) {

case 0: {

rotationResult = FirebaseVisionImageMetadata.ROTATION_0;

break;

}

case 90: {

rotationResult = FirebaseVisionImageMetadata.ROTATION_90;

break;

}

case 180: {

rotationResult = FirebaseVisionImageMetadata.ROTATION_180;

break;

}

case 270: {

rotationResult = FirebaseVisionImageMetadata.ROTATION_270;

break;

}

default: {

rotationResult = FirebaseVisionImageMetadata.ROTATION_0;

break;

}

}

FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()

.setWidth(imgWidth)

.setHeight(imgHeight)

.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)

.setRotation(rotationResult)

.build();

image = FirebaseVisionImage.fromByteArray(data, metadata);

Task> result = detector.detectInImage(image)

最佳答案

一些东西。

如果使用camera2,则图像格式不应为NV21。有关所有camera2支持的图像格式,请参见此处:

https://developer.android.com/reference/android/media/Image#getFormat()

您的byte []不是NV21,并且您指定了IMAGE_FORMAT_NV21并导致错误

与camera2的最直观的集成如下所示:

实例化ImageReader时指定JPEG格式。

onImageAvailable将带给您android.media.Image,您可以直接使用FirebaseVisionImage.fromMediaImage(...)创建FirebaseVisionImage。 (您可以从官方文档here中找到如何计算轮换信息)

如果必须执行两个活动,则需要解决android.media.Image不可打包的事实。我建议您先将其转换为可打包的位图,然后将其直接设置为Intent extra(由您决定。仅从最终用户的角度考虑,通常不会看到条形码被保存到我的图片库中。

因此,您可能需要考虑跳过将其保存到文件的步骤。稍后,在第二活动中,您可以使用FirebaseVisionImage.fromBitmap(...)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值