Is it possible to create a barcode scanning app that doesn't use the ZXing library as all I have found so far is that the ZXing library requires their Barcode Scanning app but I don't want my app to rely on a third party app.
Thank you
Is it possible to create a barcode scanning app that doesn't use the ZXing library as all I have found so far is that the ZXing library requires their Barcode Scanning app but I don't want my app to rely on a third party app.
Thank you
Yes u can go with google vision library for barcode scanning in your app. Checkout here
Yes It is possible, You can create your application Using google vision library only. If you use Google Pay, then You can see the qr scanner. I exactly made that, And UI Part with blur effect also. You can go through code as well.
Use this google library in build.gradle in app,
implementation 'com.google.android.gms:play-services-vision:19.0.0'
Then You have to make a UI part like this xml,
<?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>
In Ui there is BarCodeScannerView class which I draw on canvas as you can see the code;
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);
}
}
Then You have the Activity code where you open your camera and set the barcodeReader variable and get the value. and the code is ;
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();
}