您的位置:首页 > 其它

生成文字的二维码

2016-04-19 19:36 435 查看
//此代码需要关于二维码的jar包

<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"
tools:context="com.example.zxing.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:ems="10" >
<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="生成一个二维码" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="89dp"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>


package com.example.zxing;
import com.google.zxing.WriterException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity{
//定义控件
private EditText mEditText;
private Button button;
private ImageView image;
protected int mScreenWidth ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得控件的id
mEditText=(EditText) findViewById(R.id.editText1);
button=(Button) findViewById(R.id.button1);
image=(ImageView) findViewById(R.id.imageView1);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mScreenWidth = dm.widthPixels;
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String string = mEditText.getText().toString();

Bitmap bitmap;
try {
bitmap = BitmapUtil.createQRCode(string, mScreenWidth);
if(bitmap != null){
image.setImageBitmap(bitmap);
}
} catch (WriterException e) {
e.printStackTrace();
}

}
});
}
}


package com.example.zxing;

import java.util.Hashtable;
import android.graphics.Bitmap;
import android.util.Log;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class BitmapUtil{
/**
* 生成一个二维码图像
*
* @param url
*            传入的字符串,通常是一个URL
* @param QR_WIDTH
*            宽度(像素值px)
* @param QR_HEIGHT
*            高度(像素值px)
* @return
*/
public static final Bitmap create2DCoderBitmap(String url, int QR_WIDTH,
int QR_HEIGHT) {
try {
// 判断URL合法性
if (url == null || "".equals(url) || url.length() < 1) {
return null;
}
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 图像数据转换,使用了矩阵转换
BitMatrix bitMatrix = new QRCodeWriter().encode(url,
BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
// 下面这里按照二维码的算法,逐个生成二维码的图片,
// 两个for循环是图片横列扫描的结果
for (int y = 0; y < QR_HEIGHT; y++){
for (int x = 0; x < QR_WIDTH; x++){
if (bitMatrix.get(x, y)) {
pixels[y * QR_WIDTH + x] = 0xff000000;
} else {
pixels[y * QR_WIDTH + x] = 0xffffffff;
}
}
}
// 生成二维码图片的格式,使用ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
// 显示到一个ImageView上面
// sweepIV.setImageBitmap(bitmap);
return bitmap;
} catch (WriterException e) {
Log.i("log", "生成二维码错误" + e.getMessage());
return null;
}
}

private static final int BLACK = 0xff000000;

/**
* 生成一个二维码图像
*
* @param url
*            传入的字符串,通常是一个URL
* @param widthAndHeight
*           图像的宽高
* @return
*/
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: