条形码扫描仪应用程序安卓不使用ZXing库



是否可以创建一个不使用ZXing库的条形码扫描应用程序,因为到目前为止我发现ZXing库需要他们的条形码扫描应用程序,但我不希望我的应用程序依赖于第三方应用程序。

谢谢

是的,您可以使用谷歌视觉库在您的应用程序中进行条形码扫描。 在这里结帐

是的,您可以仅使用谷歌视觉库创建应用程序。如果您使用谷歌支付,那么您可以看到二维码扫描仪。我正是这样做的,以及具有模糊效果的UI部分。您也可以浏览代码。

在应用程序中的build.gradle中使用此谷歌库,

implementation 'com.google.android.gms:play-services-vision:19.0.0'

然后你必须做一个像这个 xml 这样的 UI 部分,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.scanqrcode.BarcodeReaderActivity">
<include layout="@layout/toolbar_base" />
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/txtBarcodeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:text="No Barcode Detected"
android:textColor="@android:color/white"
android:textSize="20sp" />
<com.techkyoso.denTech.utils.QRCodeScannerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/txtScanCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Scan QR code"
android:layout_margin="@dimen/activity_vertical_margin_8dp"
android:layout_above="@id/ivScannerIcon"
android:textColor="@android:color/white"
android:textSize="@dimen/text_size_16sp" />

<ImageView
android:id="@+id/ivScannerIcon"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/activity_horizontal_margin_32dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_qr"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>

在 Ui 中,有 BarCodeScannerView 类,我在画布上绘制,因为您可以看到代码;

public class QRCodeScannerView extends View {
private Paint mPaint;
private Paint mStrokePaint;
private Path mPath = new Path();
private Point pTopLeft = new Point();
private Point pBotRight = new Point();
public QRCodeScannerView(Context context) {
super(context);
initPaints();
}
public QRCodeScannerView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaints();
}
public QRCodeScannerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaints();
}
private void initPaints() {
mPaint = new Paint();
mPaint.setColor(Color.parseColor("#A6000000"));
mStrokePaint = new Paint();
mStrokePaint.setColor(Color.YELLOW);
mStrokePaint.setStrokeWidth(4);
mStrokePaint.setStyle(Paint.Style.STROKE);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
pTopLeft.x = (getWidth() / 13)+10;
pTopLeft.y = (getHeight() / 4)+20;
pBotRight.x = (getWidth() - pTopLeft.x)-10;
pBotRight.y = (getHeight() - pTopLeft.y)-20;
mPaint.setColor(Color.parseColor("#77000000"));
//Top Rect
canvas.drawRect(0, 0, getWidth(), pTopLeft.y, mPaint);
//Left Rect
canvas.drawRect(0, pTopLeft.y, pTopLeft.x, pBotRight.y, mPaint);
//Right Rect
canvas.drawRect(pBotRight.x, pTopLeft.y, getWidth(), pBotRight.y, mPaint);
//Bottom rect
canvas.drawRect(0, pBotRight.y, getWidth(), getHeight(), mPaint);
//Draw Outer Line drawable
Drawable d = getResources().getDrawable(R.drawable.scanner_outline, null);
d.setBounds(pTopLeft.x, pTopLeft.y, pBotRight.x, pBotRight.y);
d.draw(canvas);
}
}

然后,您有活动代码,您可以在其中打开相机并设置条形码阅读器变量并获取值。 代码是 ;

public class BarcodeReaderActivity extends AppCompatActivity  {
SurfaceView surfaceView;
TextView txtBarcodeValue;
private BarcodeDetector barcodeDetector;
private CameraSource cameraSource;
private static final int REQUEST_CAMERA_PERMISSION = 201;
Button btnAction;
String intentData = "";
boolean isEmail = false;
Toolbar toolBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode_reader);
toolBar = findViewById(R.id.toolbar);
setSupportActionBar(toolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Scan QR Code");
toolBar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
toolBar.setNavigationOnClickListener(view -> onBackPressed());
initViews();
}
private void initViews() {
txtBarcodeValue = findViewById(R.id.txtBarcodeValue);
surfaceView = findViewById(R.id.surfaceView);
}
private void initialiseDetectorsAndSources() {
Toast.makeText(getApplicationContext(), "Barcode scanner started", Toast.LENGTH_SHORT).show();
barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build();
cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(1920, 1080)
.setAutoFocusEnabled(true) //you should add this feature
.build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(BarcodeReaderActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
cameraSource.start(surfaceView.getHolder());
} else {
ActivityCompat.requestPermissions(BarcodeReaderActivity.this, new
String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});

barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
Intent data = new Intent();
data.putExtra("FirstValue", barcodes.valueAt(0).displayValue);
setResult(RESULT_OK, data);
finish();
}
}
});
}

@Override
protected void onPause() {
super.onPause();
cameraSource.release();
}
@Override
protected void onResume() {
super.onResume();
initialiseDetectorsAndSources();
}

最新更新