zbar android本地图片,ZBar Android扫描本地QR或条形码图像

程卓君
2023-12-01

5 个答案:

答案 0 :(得分:4)

试试这个:

Bitmap barcodeBmp = BitmapFactory.decodeResource(getResources(),

R.drawable.barcode);

int width = barcodeBmp.getWidth();

int height = barcodeBmp.getHeight();

int pixels = new int;

barcodeBmp.getPixels(pixels, 0, width, 0, 0, width, height);

Image barcode = new Image(width, height, "RGB4");

barcode.setData(pixels);

int result = scanner.scanImage(barcode.convert("Y800"));

答案 1 :(得分:1)

我不清楚知道Android,但在iOS上执行:

//Action when user tap on button to call ZBarReaderController

- (IBAction)brownQRImageFromAlbum:(id)sender {

ZBarReaderController *reader = [ZBarReaderController new];

reader.readerDelegate = self;

reader.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // Set ZbarReaderController point to the local album

ZBarImageScanner *scanner = reader.scanner;

[scanner setSymbology: ZBAR_QRCODE

config: ZBAR_CFG_ENABLE

to: 1];

[self presentModalViewController: reader animated: YES];

}

- (void) imagePickerController: (UIImagePickerController *) picker

didFinishPickingMediaWithInfo: (NSDictionary *) info {

UIImage *imageCurrent = (UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage];

self.imageViewQR.image = imageCurrent;

imageCurrent = nil;

// ADD: get the decode results

id results =

[info objectForKey: ZBarReaderControllerResults];

ZBarSymbol *symbol = nil;

for (symbol in results)

break;

NSLog(@"Content: %@", symbol.data);

[picker dismissModalViewControllerAnimated: NO];

}

答案 2 :(得分:1)

Zbar扫描仪的Java端口仅接受Y800和GRAY像素格式(https://github.com/ZBar/ZBar/blob/master/java/net/sourceforge/zbar/ImageScanner.java),这对于从相机预览中捕获的原始字节是可以的。但是来自Androis画廊的图像通常是JPEG压缩的,它们的像素不在Y800中,因此您可以通过将图像的像素转换为Y800格式来使扫描仪工作。有关示例代码,请参阅this官方支持论坛的主题。要计算像素数组长度,只需使用imageWidth * imageHeight公式。

@shujatAli您的示例图像调色板的格式是灰度,将其转换为RGB使您的代码片段适合我。您可以使用某些图像处理程序更改托盘的格式。我用过GIMP。

答案 3 :(得分:0)

private ImageScanner mScanner = new ImageScanner();

/**

* using zbar scan the bitmap

*/

private void scanQRImageZbar(Bitmap barcodeBmp) {

int width = barcodeBmp.getWidth();

int height = barcodeBmp.getHeight();

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

barcodeBmp.getPixels(pixels, 0, width, 0, 0, width, height);

Image barcode = new Image(width, height, "RGB4");

barcode.setData(pixels);

int result = mScanner.scanImage(barcode.convert("Y800"));

if (result != 0) {

// save result in @var symSet

SymbolSet symSet = mScanner.getResults();

final Result rawResult = new Result();

for (Symbol sym : symSet) {

// symData will have the resultant code value

String symData;

if (Build.VERSION.SDK_INT >= 19) {

symData = new String(sym.getDataBytes(), StandardCharsets.UTF_8);

} else {

symData = sym.getData();

}

// save it in rawResult

if (!TextUtils.isEmpty(symData)) {

rawResult.setContents(symData);

rawResult.setBarcodeFormat(BarcodeFormat.getFormatById(sym.getType()));

break;

}

}

// toast message of content

new AlertDialog.Builder(FullScannerActivity.this).setMessage(rawResult.getContents()).show();

Log.e("onPreviewFrame: ", rawResult.getContents());

}

}

答案 4 :(得分:-2)

#include

#include

#include

using namespace std;

using namespace zbar;

int main (int argc, char **argv)

{

if(argc < 2)

return(1);

// Create a reader

ImageScanner scanner;

// Configure the reader

scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

// Obtain image data

Magick::Image magick(argv[1]); // Read an image file

int width = magick.columns(); // Extract dimensions

int height = magick.rows();

Magick::Blob blob; // Extract the raw data

magick.modifyImage();

magick.write(&blob, "GRAY", 8);

const void *raw = blob.data();

// Wrap image data

Image image(width, height, "Y800", raw, width * height);

// Scan the image for barcodes

int n = scanner.scan(image);

// Extract results

for (Image::SymbolIterator symbol = image.symbol_begin();

symbol != image.symbol_end();

++symbol) {

// Do something useful with results

cout << "decoded " << symbol->get_type_name()

<< " symbol \"" << symbol->get_data() << '"' << endl;

}

// Clean up

image.set_data(NULL, 0);

return(0);

}

按照上面的代码进行更改,使其与您的语言编程相关。

 类似资料: