android扫码library对比,android – 如何使用Zxing Library获取扫描图片和QR码

本文介绍如何在Android项目中使用ZxingLibrary正确扫描二维码,包括设置扫描参数、处理扫描结果及保存图像数据。重点在于解决扫描过程中遇到的问题和代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我有一个

github项目使用Zxing Library扫描QR码,但不知怎的,我也无法获得扫描图像和QR码.这是我的代码.我在使用gradle:

> Gradle编译语句

的build.gradle

compile 'me.dm7.barcodescanner:core:1.9'

compile 'com.google.zxing:core:3.2.1'

您可以在下面看到我的活动和xml代码.

ScanQR_Code_Activity.java

package com.rishi.myaadhaar.activities;

import android.os.Bundle;

import android.os.Handler;

import android.support.v7.app.AppCompatActivity;

import android.view.ViewGroup;

import android.widget.Toast;

import com.google.zxing.Result;

import com.myadhaar.R;

import java.util.ArrayList;

public class ScanQR_Code_Activity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

private static final String FLASH_STATE = "FLASH_STATE";

// UI Elements

private static final String AUTO_FOCUS_STATE = "AUTO_FOCUS_STATE";

private static final String SELECTED_FORMATS = "SELECTED_FORMATS";

private static final String CAMERA_ID = "CAMERA_ID";

String uid, name, gender, yearOfBirth, careOf, villageTehsil, postOffice, house, street, loc, district, state, postCode, lm;

ViewGroup contentFrame;

private ZXingScannerView mScannerView;

private boolean mFlash;

private boolean mAutoFocus;

private ArrayList mSelectedIndices;

private int mCameraId = -1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_scan_qr_code);

if (savedInstanceState != null) {

mFlash = savedInstanceState.getBoolean(FLASH_STATE, false);

mAutoFocus = savedInstanceState.getBoolean(AUTO_FOCUS_STATE, true);

mSelectedIndices = savedInstanceState.getIntegerArrayList(SELECTED_FORMATS);

mCameraId = savedInstanceState.getInt(CAMERA_ID, -1);

} else {

mFlash = false;

mAutoFocus = true;

mSelectedIndices = null;

mCameraId = -1;

}

contentFrame = (ViewGroup) findViewById(R.id.content_frame);

mScannerView = new ZXingScannerView(this);

contentFrame.addView(mScannerView);

}

@Override

public void onResume() {

super.onResume();

mScannerView.setResultHandler(this);

mScannerView.startCamera();

mScannerView.setFlash(mFlash);

mScannerView.setAutoFocus(mAutoFocus);

}

@Override

public void onPause() {

super.onPause();

mScannerView.stopCamera();

}

@Override

public void handleResult(Result rawResult) {

Toast.makeText(this, "Contents = " + rawResult.getText() +

", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();

if (rawResult != null && !rawResult.getText().toString().isEmpty()) {

//processScannedData(rawResult.getText());

} else {

Toast toast = Toast.makeText(getApplicationContext(), "Scan Cancelled", Toast.LENGTH_SHORT);

toast.show();

}

// Note:

// * Wait 2 seconds to resume the preview.

// * On older devices continuously stopping and resuming camera preview can result in freezing the app.

// * I don't know why this is the case but I don't have the time to figure out.

Handler handler = new Handler();

handler.postDelayed(new Runnable() {

@Override

public void run() {

mScannerView.resumeCameraPreview(ScanQR_Code_Activity.this);

}

}, 2000);

}

}

activity_scan_qr_code.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_scan_qr_code"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.rishi.myaadhaar.activities.ScanQR_Code_Activity">

android:id="@+id/content_frame"

android:layout_width="match_parent"

android:layout_height="match_parent" />

扩展课程

public class ZXingScannerView extends BarcodeScannerView {

private static final String TAG = "ZXingScannerView";

public interface ResultHandler {

public void handleResult(Result rawResult);

}

private MultiFormatReader mMultiFormatReader;

public static final List ALL_FORMATS = new ArrayList();

private List mFormats;

private ResultHandler mResultHandler;

static {

ALL_FORMATS.add(BarcodeFormat.UPC_A);

ALL_FORMATS.add(BarcodeFormat.UPC_E);

ALL_FORMATS.add(BarcodeFormat.EAN_13);

ALL_FORMATS.add(BarcodeFormat.EAN_8);

ALL_FORMATS.add(BarcodeFormat.RSS_14);

ALL_FORMATS.add(BarcodeFormat.CODE_39);

ALL_FORMATS.add(BarcodeFormat.CODE_93);

ALL_FORMATS.add(BarcodeFormat.CODE_128);

ALL_FORMATS.add(BarcodeFormat.ITF);

ALL_FORMATS.add(BarcodeFormat.CODABAR);

ALL_FORMATS.add(BarcodeFormat.QR_CODE);

ALL_FORMATS.add(BarcodeFormat.DATA_MATRIX);

ALL_FORMATS.add(BarcodeFormat.PDF_417);

}

public ZXingScannerView(Context context) {

super(context);

initMultiFormatReader();

}

public ZXingScannerView(Context context, AttributeSet attributeSet) {

super(context, attributeSet);

initMultiFormatReader();

}

public void setFormats(List formats) {

mFormats = formats;

initMultiFormatReader();

}

public void setResultHandler(ResultHandler resultHandler) {

mResultHandler = resultHandler;

}

public Collection getFormats() {

if(mFormats == null) {

return ALL_FORMATS;

}

return mFormats;

}

private void initMultiFormatReader() {

Map hints = new EnumMap(DecodeHintType.class);

hints.put(DecodeHintType.POSSIBLE_FORMATS, getFormats());

mMultiFormatReader = new MultiFormatReader();

mMultiFormatReader.setHints(hints);

}

@Override

public void onPreviewFrame(byte[] data, Camera camera) {

if(mResultHandler == null) {

return;

}

try {

Camera.Parameters parameters = camera.getParameters();

Camera.Size size = parameters.getPreviewSize();

int width = size.width;

int height = size.height;

if (DisplayUtils.getScreenOrientation(getContext()) == Configuration.ORIENTATION_PORTRAIT) {

byte[] rotatedData = new byte[data.length];

for (int y = 0; y < height; y++) {

for (int x = 0; x < width; x++)

rotatedData[x * height + height - y - 1] = data[x + y * width];

}

int tmp = width;

width = height;

height = tmp;

data = rotatedData;

}

Result rawResult = null;

PlanarYUVLuminanceSource source = buildLuminanceSource(data, width, height);

if (source != null)

{

BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

try {

rawResult = mMultiFormatReader.decodeWithState(bitmap);

} catch (ReaderException re) {

// continue

} catch (NullPointerException npe) {

// This is terrible

} catch (ArrayIndexOutOfBoundsException aoe) {

} finally

{

mMultiFormatReader.reset();

}

}

final Result finalRawResult = rawResult;

if (finalRawResult != null) {

Handler handler = new Handler(Looper.getMainLooper());

handler.post(new Runnable() {

@Override

public void run() {

// Stopping the preview can take a little long.

// So we want to set result handler to null to discard subsequent calls to

// onPreviewFrame.

ResultHandler tmpResultHandler = mResultHandler;

mResultHandler = null;

stopCameraPreview();

if (tmpResultHandler != null) {

tmpResultHandler.handleResult(finalRawResult);

}

}

});

} else {

camera.setOneShotPreviewCallback(this);

}

} catch(RuntimeException e) {

// TODO: Terrible hack. It is possible that this method is invoked after camera is released.

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

}

}

public void resumeCameraPreview(ResultHandler resultHandler) {

mResultHandler = resultHandler;

super.resumeCameraPreview();

}

public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {

Rect rect = getFramingRectInPreview(width, height);

if (rect == null)

{

return null;

}

// Go ahead and assume it's YUV rather than die.

PlanarYUVLuminanceSource source = null;

try

{

source = new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);

}

catch(Exception e)

{

}

return source;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值