Java实现二维码QRCode的编码和解码
http://sjsky.iteye.com/blog/1136934
Android平台利用ZXING生成二维码图片
http://blog.csdn.net/zz880329/article/details/6684025#
java生成二维码的三个工具
http://ylq365.iteye.com/blog/1160635
参考文章
Android及Java使用google.zxing生成和解码(解析)二维码(QRCode)
http://orgcent.com/android-zxing-qrcode-encode-barcode/
1、J2SE下二维码图像生成
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE,width, height);
File file = new File(imagePath);
MatrixToImageWriter.writeToFile(matrix, "png", file);
} catch (WriterException e) {
e.printStackTrace();
}
2、Android系统下二维码图像生成
package com.orgcent.zxing;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
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;
/**
*
* Android中二维码编码及图像生成
*
*/
public class QRCodeCreatorActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String content = "http://orgcent.com/";
final int desiredWidth = 120;
final int desiredHeight = 120;
final String imageFileName = "orgcent.com.png";
FileOutputStream fos = null;
Bitmap bitmap = null;
try {//生成二维码图像
bitmap = encodeAsBitmap(content, BarcodeFormat.QR_CODE, desiredWidth, desiredHeight);
if(null != bitmap) {//将二维码图像保存到文件
File file = new File(Environment.getExternalStorageDirectory(), imageFileName);
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != fos) {
try {
fos.close();
} catch (Exception e) {}
}
}
//显示QRCode
if(null != bitmap) {
ImageView iv = new ImageView(this);
iv.setImageBitmap(bitmap);
iv.setScaleType(ScaleType.FIT_CENTER);
setContentView(iv,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
static Bitmap encodeAsBitmap(String contents, BarcodeFormat format,
int desiredWidth, int desiredHeight) throws WriterException {
final int WHITE = 0xFFFFFFFF; //可以指定其他颜色,让二维码变成彩色效果
final int BLACK = 0xFF000000;
HashMap<EncodeHintType, String> hints = null;
String encoding = guessAppropriateEncoding(contents);
if (encoding != null) {
hints = new HashMap<EncodeHintType, String>(2);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, format, desiredWidth,
desiredHeight, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
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;
}
private static String guessAppropriateEncoding(CharSequence contents) {
// Very crude at the moment
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) {
return "UTF-8";
}
}
return null;
}
}