android实现QRcode生成

开发QRcode生成的APP,需要引用google.zxing项目——即把core.jar文件放在libs文件夹下,并导入项目,不然代码会报错!

布局文件(.xml):
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”top”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.first.MainActivity” >

<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/editText1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”31dp”
android:text=”@string/btn_info” />

<EditText
android:id=”@+id/editText1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”18dp”
android:ems=”10″ >

<requestFocus />
</EditText>

<ImageView
android:id=”@+id/imageView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/button1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”83dp”
android:src=”@drawable/ic_launcher” />

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_alignRight=”@+id/imageView1″
android:layout_marginTop=”47dp”
android:text=”@string/QRcode” />

</RelativeLayout>

Activity类(.java):
package com.example.first;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView txt;
private Button btn;
private EditText edit;
private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//获取layout中的控件对象
txt = (TextView) this.findViewById(R.id.textView1);
btn = (Button)this.findViewById(R.id.button1);
edit = (EditText)this. findViewById(R.id.editText1);
img = (ImageView)this. findViewById(R.id.imageView1);

//txt.setText(R.string.change_info);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = edit.getText().toString();
txt.setText(str);
try {
Bitmap qrcode = create2DCode(str);
img.setImageBitmap(qrcode);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

}

/**
*根据字符串生成二维码
* @param str
* @return
* @throws WriterException
*/
public Bitmap create2DCode(String str) throws WriterException{
//生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 300, 300);
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] = 0xff000000;
}

}
}

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
}

© 2014, 李德涛博客. 版权所有.

发表评论

电子邮件地址不会被公开。 必填项已用*标注