1,activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_erweima);
initViews();
}
private void initViews() {
// TODO Auto-generated method stub
qr_show = (ImageView) findViewById(R.id.qr_show);
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
int w = outMetrics.widthPixels * 8 / 11;//设置宽度
ViewGroup.LayoutParams layoutParams = qr_show.getLayoutParams();
layoutParams.height = layoutParams.width = w;//设置高度
qr_show.setLayoutParams(layoutParams);
try {
Bitmap bitmap = QRUtils.encodeToQRWidth("http://write.blog.csdn.net/", w);//要生成二维码的内容,我这就是一个网址
qr_show.setImageBitmap(bitmap);
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "生成二维码失败", Toast.LENGTH_SHORT);
}
}
2,activity_erweima.xml文件
<?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:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
>
<ImageView
android:id="@+id/qr_show"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="55dp"
android:scaleType="fitXY"
android:background="@null"
android:src="@drawable/ic_launcher"></ImageView>
</LinearLayout>
3,生成类
package cn.test.tools;
import java.util.EnumMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.text.TextUtils;
public class QRUtils {
private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;
/**
* 将字符串按照指定大小生成二维码图片
*/
public static Bitmap encodeToQR(String contentsToEncode, int dimension)
throws Exception {
if (TextUtils.isEmpty(contentsToEncode))
return null;
BarcodeFormat format = BarcodeFormat.QR_CODE;
Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(
EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix result = new MultiFormatWriter().encode(contentsToEncode,
format, dimension, dimension, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public static Bitmap encodeToQRWidth(String contentsToEncode, int dimension)
throws Exception {
if (TextUtils.isEmpty(contentsToEncode))
return null;
BarcodeFormat format = BarcodeFormat.QR_CODE;
Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(
EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix result = new MultiFormatWriter().encode(contentsToEncode,
format, dimension, dimension, hints);
int width = result.getWidth();
int height = result.getHeight();
boolean isFirstBlack = true;
int startX = 0;
int startY = 0;
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
if (result.get(x, y) && isFirstBlack) {
isFirstBlack = false;
startX = x;
startY = y;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
Matrix m = new Matrix();
float sx = (width + 2f * startX) / width;
float sy = (height + 2f * startY) / height;
m.postScale(sx, sy);
Bitmap qrBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(qrBitmap);
canvas.translate(-startX, -startY);
Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawBitmap(bitmap, m, paint);
canvas.save();
return qrBitmap;
}
}
4,导入jar包
在本博客的文件中
参考:http://www.open-open.com/code/view/1426334688154