利用開源zxing(http://code.google.com/p/zxing/)項目生成二維碼
1.下載zxing的核心包,core.jar,一般在網上沒有直接提供此jar文件的下載,後面我將會上傳,在項目根目錄下建立libs文件夾,將下載的jar文件放在libs目錄下,注意,文件夾的名字一定要是libs,否則會報錯。
2.實現二維碼的生成,業務類
public class QRCodeService {
// 圖片大小,注意圖片不要設置太大,否則會影響二維碼的識別
private static final int IMAGE_WIDTH = 30;
/**
* 生成普通二維碼方法
*
* @param 傳入二維碼的內容
* @return Bitmap 插入到二維碼中間的位圖對象,可以傳入Null,傳入NULL後,生成的二維碼中間不帶圖片
*/
public Bitmap cretaeBitmap(String str, Bitmap mBitmap) throws Exception {
Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(
EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 生成二維矩陣,編碼時指定大小,不要生成了圖片以後再進行縮放,這樣會模糊導致識別失敗
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, 300, 300, hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
// 二維矩陣轉爲一維像素數組,也就是一直橫着排了
int halfW = width / 2;
int halfH = height / 2;
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// 如果mBitmap不爲空,則添加中間小圖片
if (mBitmap != null && x > halfW - IMAGE_WIDTH
&& x < halfW + IMAGE_WIDTH && y > halfH - IMAGE_WIDTH
&& y < halfH + IMAGE_WIDTH) {
pixels[y * width + x] = mBitmap.getPixel(x - halfW
+ IMAGE_WIDTH, y - halfH + IMAGE_WIDTH);
} else {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff314785;// 生成二維碼填充的顏色。黑色
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通過像素數組生成bitmap位圖
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 生成中間帶圖片的二維碼
*
* @param image
* 設置需要放在二維碼中間的圖片
* @content 傳入二維碼的內容
* @resources Resources獲得對象
*/
public Bitmap createImageViewQRBitmap(int image, String content,
Resources resources) throws Exception {
// 構造需要插入的位圖對象
Bitmap mBitmap = ((BitmapDrawable) resources.getDrawable(image))
.getBitmap();
// 進行縮放圖片
Matrix m = new Matrix();
float sx = (float) 2 * IMAGE_WIDTH / mBitmap.getWidth();
float sy = (float) 2 * IMAGE_WIDTH / mBitmap.getHeight();
m.setScale(sx, sy);
// 重新構造位圖對象
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(),
mBitmap.getHeight(), m, true);
mBitmap = cretaeBitmap(content, mBitmap);
return mBitmap;
}
}
3.將生成的二維碼保存在SD卡上
/**
* 將生成的二維碼保存在SD卡
*/
public class QRToSDcardSaveService {
/**
* 將位圖對象轉換爲字節數組
* @param bm
* @return
*/
private byte[] Bitmap2Bytes(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
return outputStream.toByteArray();
}
/**
* 保存二維碼至SD卡
* @param filename
* @param bitmap
*/
public void saveToSDCard(String filename, Bitmap bitmap) throws Exception {
// 獲取SD卡的路徑:Environment.getExternalStorageDirectory()
File file = new File(Environment.getExternalStorageDirectory(),
filename);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(Bitmap2Bytes(bitmap));
outStream.close();
}
}
4.調用
public class MyCardActivity extends Activity {
private QRCodeService service;
private QRToSDcardSaveService dcardSaveService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView=(ImageView)this.findViewById(R.id.card);//用來顯示二維碼的視圖
service=new QRCodeService();//生成二維碼的業務類
dcardSaveService=new QRToSDcardSaveService();//保存二維碼至SD卡的業務類
try {
//生成二維碼
Bitmap b=service.createImageViewQRBitmap(R.drawable.ic_launcher, "要存入的信息",getResources());
imageView.setImageBitmap(b);//將生成的二維碼顯示在視圖控件上
dcardSaveService.saveToSDCard("ggg1.png", b);//將生成的二維碼保存在SD卡上
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, R.string.error, 1).show();
}
}
}
注意:將二維碼保存在SD卡上需要寫入權限,在項目清單文件中增加
<!-- 往SDCard寫入數據權限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
源碼下載地址:android實現二維碼源碼下載