introduce
I believe that Adnroid development knows that there are four scanners, Zxing, Zbar, Barcode4J, Okapi Barcode and the former two are widely used.
In two-dimensional code and bar code scanning on android, google officially provided us with zXing. Almost all android related scans were implemented by this open source project, and also used zBar on android. Other used exchanges show that zBar is better than zXing in scanning barcode, zXing is better in barcode, and ios generally uses zBar.
This paper uses ZXing 1.6 to realize barcode/two-dimensional code recognition. ZXing is a classic open source class library for barcode/two-dimensional code recognition. Developers have used ZXing on J2ME for a long time, but mobile phones that support the JSR-234 specification (autofocus) can exert their power. At present, many Android phones have the function of autofocus.
Introduction to ZXing
ZXing is an open source Java class library for parsing barcodes and two-dimensional codes in multiple formats
Here is also a newly packaged ZXing Lib:
Click to view
analysis
CaptureActivity
Call Activity exposed by ZXing. In the handleDecode method, the action after successful scan is processed.
ViewfinderView
In the open source library provided above, the author extracts the drawing of the scan box directly into the XML file, which makes it more convenient to modify.
CameraConfigurationManager
Modify the core class of horizontal and vertical screen and deal with deformation effect.
In the public void setDesired Camera Parameters (Camera camera, Boolean safe mode) method (read configuration setup camera focus mode, flash mode, etc.), scanning can be changed to vertical screen.
In the public void initFromCamera Parameters (Camera camera) method (which calculates the screen resolution and the most suitable camera pixels at present), we can modify it to vertical screen scan code, because the pixel information points do not modify the image distortion caused by the adjustment.
DecodeHandler.decode
Core Classes of ZXing Decoding
CaptureActivityHandler
When DecodeHandler.decode completes decoding, the system sends a message to CaptureActivityHandler. If the encoding is successful, the CaptureActivity.handleDecode method is called to classify the scanned results.
<?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="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Start Scan"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"/>
<TextView
android:id="@+id/tv_scan_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="18sp"/>
</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"/>
<CheckBox
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logo"/>
<ImageView
android:id="@+id/iv_qr_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@mipmap/ic_launcher"/>
</LinearLayout>
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;
public class MainActivity extends Activity {
private TextView resultTextView;
private EditText qrStrEditText;
private ImageView qrImgImageView;
private CheckBox mCheckBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
mCheckBox = (CheckBox) findViewById(R.id.logo);
Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
scanBarCodeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Open the scanning interface to scan barcodes or two-dimensional codes
Intent openCameraIntent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(openCameraIntent, 0);
}
});
Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);
generateQRCodeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String contentString = qrStrEditText.getText().toString();
if (!contentString.equals("")) {
//The second parameter is the size of the picture.(350*350)
Bitmap qrCodeBitmap = EncodingUtils.createQRCode(contentString, 350, 350,
mCheckBox.isChecked() ?
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher) :
null);
qrImgImageView.setImageBitmap(qrCodeBitmap);
} else {
Toast.makeText(MainActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String scanResult = bundle.getString("result");
resultTextView.setText(scanResult);
}
}
}