i would like to implement a QR Code/Barcode reader within my application. I would like to know what is the most lightweight solution to do this (disregarding intent integrator from zxing).
If nothing happens, you may have to go to your Settings app and enable QR Code scanning. If QR Codes isn't an option in your settings, your device unfortunately can't scan QR Codes natively. But don't worry, this only means you'll have to download a third-party QR Code reader app (see our app recommendations below).
Your phone's camera may have trouble scanning the code if it's tilted at an angle. Make sure it's level with the surface that the code is printed on. If you're holding your phone too close or too far away, it won't scan the code. Try holding your phone about a foot away and slowly moving it towards the QR code.
It's not possible to reactivate a QR Code by creating and upgrading a new account. It's also not possible to reactivate another QR Code by creating a new QR Code with the same content.
Does Android have an in-built QR Code reader? Yes. Just like iPhones, Android 9 (Android Pie) and Android 10 have an in-built QR Code reader. Even the Android 8 or Oreo does not need an app to scan QR Codes.
I used zxing to build into my application. You will need a bit of coding. First include core.jar , its at core/core.jar,in your build path, then go to their client ,its at android/..../com.google.zxing, and get their code(This is not recommended by the devs, because your copy and pasting.) last, Add this code:
package com.wtsang02.activities;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.HybridBinarizer;
public class QRDecoder extends Activity implements OnClickListener {
private String text;
private Button webbutton;
private Bitmap bmp;
private ImageView ivPicture;
private TextView textv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mysales);
webbutton = (Button)findViewById(R.id.webbutton);
ivPicture = (ImageView) findViewById(R.id.ivPicture);
textv= (TextView) findViewById(R.id.mytext);
webbutton.setOnClickListener(this);
}
private void decode() {
if (bmp == null) {
Log.i("tag", "wtf");
}
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
int[] intArray = new int[bmp.getWidth() * bmp.getHeight()];
bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),
bmp.getHeight());
LuminanceSource source = new com.google.zxing.RGBLuminanceSource(
bmp.getWidth(), bmp.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
try {
Result result = reader.decode(bitmap);
text = result.getText();
byte[] rawBytes = result.getRawBytes();
BarcodeFormat format = result.getBarcodeFormat();
ResultPoint[] points = result.getResultPoints();
textv.setText(text);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
Log.i("done", "done");
if(text!=null)
Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();
else{
Toast.makeText(getBaseContext(), "QQ", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
ivPicture.setImageBitmap(bmp);
decode();
}
}
}
This code will use your phone's default camera, if you need to use their client, you will need to start their CaptureActivity
, Your layout should include a TextView
to show results, ImageView
to show the image you captured, and Button
to start the camera. . This is based off of 2.1zxing.
You can use:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With