二维码扫描生成的简单使用

1、需要文件 zxing.aar文件

链接:http://pan.baidu.com/s/1mhN1df6 密码:c6ch

2、创建一个项目
3、将下载下载的aar文件拷贝到libs文件夹中
4、在build.gradle里面的android下添加
  1. repositories {
  2. flatDir {
  3. dirs 'libs'
  4. }
  5. }

5、build.gradle里面的dependencies下添加
zxing3.1 是aar文件的名字
  1. compile(name: 'zxing3.1', ext: 'aar')
6、创建你想要显示的布局文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@android:color/white"
  6. android:orientation="vertical">
  7. <Button
  8. android:id="@+id/btn_scan_barcode"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginTop="30dp"
  12. android:text="Start Scan"/>
  13. <LinearLayout
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_marginTop="10dp"
  17. android:orientation="horizontal">
  18. <TextView
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="Result:"
  22. android:textSize="18sp"/>
  23. <TextView
  24. android:id="@+id/tv_scan_result"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:textColor="@android:color/black"
  28. android:textSize="18sp"/>
  29. </LinearLayout>
  30. <EditText
  31. android:id="@+id/et_qr_string"
  32. android:layout_width="fill_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_marginTop="30dp"
  35. android:hint="Input the text"/>
  36. <Button
  37. android:id="@+id/btn_add_qrcode"
  38. android:layout_width="fill_parent"
  39. android:layout_height="wrap_content"
  40. android:text="Generate QRcode"/>
  41. <CheckBox
  42. android:id="@+id/logo"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="Logo"/>
  46. <ImageView
  47. android:id="@+id/iv_qr_image"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_gravity="center"
  51. android:layout_marginTop="10dp"
  52. android:background="@mipmap/ic_launcher"/>
  53. </LinearLayout>
布局效果图如下



7、创建java文件编写具体逻辑

  1. package lpc.com.zxingdemo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.CheckBox;
  11. import android.widget.EditText;
  12. import android.widget.ImageView;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import com.xys.libzxing.zxing.activity.CaptureActivity;
  16. import com.xys.libzxing.zxing.encoding.EncodingUtils;
  17. public class MainActivity extends Activity {
  18. private TextView resultTextView;
  19. private EditText qrStrEditText;
  20. private ImageView qrImgImageView;
  21. private CheckBox mCheckBox;
  22. @Override
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
  27. qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
  28. qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
  29. mCheckBox = (CheckBox) findViewById(R.id.logo);
  30. Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
  31. scanBarCodeButton.setOnClickListener(new OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. //打开扫描界面扫描条形码或二维码
  35. Intent openCameraIntent = new Intent(MainActivity.this, CaptureActivity.class);
  36. startActivityForResult(openCameraIntent, 0);
  37. }
  38. });
  39. Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);
  40. generateQRCodeButton.setOnClickListener(new OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. String contentString = qrStrEditText.getText().toString();
  44. if (!contentString.equals("")) {
  45. //根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350)
  46. Bitmap qrCodeBitmap = EncodingUtils.createQRCode(contentString, 350, 350,
  47. mCheckBox.isChecked() ?
  48. BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher) :
  49. null);
  50. qrImgImageView.setImageBitmap(qrCodeBitmap);
  51. } else {
  52. Toast.makeText(MainActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
  53. }
  54. }
  55. });
  56. }
  57. @Override
  58. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  59. super.onActivityResult(requestCode, resultCode, data);
  60. if (resultCode == RESULT_OK) {
  61. Bundle bundle = data.getExtras();
  62. String scanResult = bundle.getString("result");
  63. resultTextView.setText(scanResult);
  64. }
  65. }
  66. }

8、最后实现的效果







原文地址:https://www.cnblogs.com/liupengcheng/p/5667690.html