I used the QR code scanning technology in my project. I used the zxing open-source project provided by Google, which provides QR code and barcode scanning. Scanning a barcode is to directly read the content of the barcode. Scanning a QR code is encoded and decoded according to the specified QR code format.
You can:
The encoding package is added by myself based on it. The function is to generate a QR code Image Based on the input string and return a bitmap. The other packages are included in the zxing project. In addition, I also modified the layout of the scan interface. The official scan interface is horizontal. I changed it to vertical, and added the tab and cancel button (camera. XML), in addition, some files are colors. XML, IDs. XML, these are self-contained in the original zxing project, and finally the jar package under libs.
Let's take a look at the final effect:
First, generate a QR code image (left) based on the input string, and then scan the QR code image to display the scan result (right) on the interface ):
Click the open camera button to open the scan box (picture on the left). The scan result is as follows (picture on the right ):
Next, let's take a look at how to use it. First, copy some files in the zxing project to our own project, and then Configure permissions in the mainifest file:
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />
There is also the configuration of the scan interface activity:
<activity android:configChanges="orientation|keyboardHidden" android:name="com.zxing.activity.CaptureActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" > </activity>
Next is the layout file of my own project:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:orientation="vertical" > <Button android:id="@+id/btn_scan_barcode" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="Open camera" /> <LinearLayout android:orientation="horizontal" android:layout_marginTop="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/black" android:textSize="18sp" android:text="Scan result:" /> <TextView android:id="@+id/tv_scan_result" android:layout_width="fill_parent" android:textSize="18sp" android:textColor="@android:color/black" android:layout_height="wrap_content" /> </LinearLayout> <EditText android:id="@+id/et_qr_string" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:hint="Input the text"/> <Button android:id="@+id/btn_add_qrcode" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Generate QRcode" /> <ImageView android:id="@+id/iv_qr_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_gravity="center"/></LinearLayout>
The following is the main activity code. The main function is to open the scan box, display the scan results, and generate a QR code Image Based on the input string:
Public class barcodetestactivity extends activity {/** called when the activity is first created. */private textview resulttextview; private edittext qrstredittext; private imageview qrimgimageview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); resulttextview = (textview) This. findviewbyid (R. id. TV _scan_result); qrstredi Ttext = (edittext) This. findviewbyid (R. id. et_qr_string); qrimgimageview = (imageview) This. findviewbyid (R. id. iv_qr_image); button scanbarcodebutton = (button) This. findviewbyid (R. id. btn_scan_barcode); scanbarcodebutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// open the scan interface to scan the barcode or QR code intent opencameraintent = new intent (barcodetestactivity. this, captureactivity. class); Startactivityforresult (opencameraintent, 0) ;}}); button generateqrcodebutton = (button) This. findviewbyid (R. id. btn_add_qrcode); generateqrcodebutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {try {string contentstring = qrstredittext. gettext (). tostring (); If (! Contentstring. equals ("") {// generate a QR code Image Based on the string and display it on the interface. The second parameter is the image size (350*350) bitmap qrcodebitmap = encodinghandler. createqrcode (contentstring, 350); qrimgimageview. setimagebitmap (qrcodebitmap);} else {toast. maketext (barcodetestactivity. this, "text can not be empty", toast. length_short ). show () ;}} catch (writerexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}) ;}@ overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {super. onactivityresult (requestcode, resultcode, data); // process the scan result (displayed on the Interface) if (resultcode = result_ OK) {bundle = data. getextras (); string scanresult = bundle. getstring ("result"); resulttextview. settext (scanresult );}}}
The code for generating the QR code is in encodinghandler. Java:
public final class EncodingHandler {private static final int BLACK = 0xff000000;public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);int width = matrix.getWidth();int height = matrix.getHeight();int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {if (matrix.get(x, y)) {pixels[y * width + x] = BLACK;}}}Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}}
Finally, go to captureactivity. Java and find the following method to perform operations on the scan results:
/** * Handler scan result * @param result * @param barcode */public void handleDecode(Result result, Bitmap barcode) {inactivityTimer.onActivity();playBeepSoundAndVibrate();String resultString = result.getText();//FIXMEif (resultString.equals("")) {Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();}else {//System.out.println("Result:"+resultString);Intent resultIntent = new Intent();Bundle bundle = new Bundle();bundle.putString("result", resultString);resultIntent.putExtras(bundle);this.setResult(RESULT_OK, resultIntent);}CaptureActivity.this.finish();}
The detailed steps of each step are not described in the above process. The overall idea is roughly as follows: Download the source code: source code
To join our QQ group or public account, see: Ryan's
Zone public account and QQ Group
Welcome to my Sina Weibo chat: @ Tang Ren _ Ryan