二维码的应用是GitHub上的开源项目。下载地址为https://github.com/zxing/zxing
我写的这个笔记Demo,主要是进行二维码扫描,以及生成二维码图标。只用到这2个功能
GitHub上的那个开源库包括的内容远远大于这2个功能。所以我从网上下载了精简版的库,只支持二维码扫面和生成的
库的地址:http://download.csdn.net/detail/u013210620/9036083
先看主页面布局
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.jikexueyuan.testqrcode.MainActivity" tools:ignore="MergeRootFrame" > <Button android:id="@+id/scan" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="開始扫描二维码" /> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show" /> <EditText android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入要编码的内容" /> <Button android:id="@+id/gen" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="生成二维码" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout>
MainActivity
package com.jikexueyuan.testqrcode; import junit.framework.Test; import com.google.zxing.WriterException; import com.zxing.activity.CaptureActivity; import com.zxing.encoding.EncodingHandler; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import android.os.Build; public class MainActivity extends Activity { //开启扫一扫 private Button scanButton; //将扫一扫后的结果正确,显示出来 private TextView text; //输入要进行生成二维码的text内容 private EditText input; //開始生成二维码 private Button genButton; //显示生成的二维码 private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scanButton = (Button) findViewById(R.id.scan); text = (TextView) findViewById(R.id.text); scanButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(MainActivity.this, "你写能够扫描条形码或者二维码", Toast.LENGTH_SHORT).show(); //开启扫一扫的activity Intent startScan = new Intent(MainActivity.this, CaptureActivity.class); //待结果返回,RESULT_OK startActivityForResult(startScan, 0); } }); input = (EditText) findViewById(R.id.input); genButton = (Button) findViewById(R.id.gen); img = (ImageView) findViewById(R.id.img); genButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String in = input.getText().toString(); if (in.equals("")) { Toast.makeText(MainActivity.this, "请输入文本", Toast.LENGTH_SHORT).show(); } else { try { //createQRCode(String str,int widthAndHeight) Bitmap qrcode = EncodingHandler.createQRCode(in, 400); //显示出来 img.setImageBitmap(qrcode); } catch (WriterException e) { e.printStackTrace(); } } } }); } /** * 扫一扫,成功后返回值进行推断 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { String result = data.getExtras().getString("result"); text.setText(result); } } }