Android Zxing生成二維碼經(jīng)典案例分享
本文實例為大家解析了Zxing生成二維碼的經(jīng)典案例,供大家參考,具體內(nèi)容如下
1、首先呢,先編譯 compile ‘com.google.zxing:core:3.2.1'
2、實戰(zhàn)
public class QRCode {
private static int IMAGE_HALFWIDTH = 50;//寬度值,影響中間圖片大小
/**
* 生成二維碼,默認大小為500*500
*
* @return bitmap
*/
public static Bitmap createQRCode() {
return createQRCode();
}
/**
* 生成二維碼,默認大小為500*500
*
* @param text 需要生成二維碼的文字、網(wǎng)址等
* @return bitmap
*/
public static Bitmap createQRCode(String text) {
return createQRCode(text, 500);
}
/**
* 生成二維碼
*
* @param text 需要生成二維碼的文字、網(wǎng)址等
* @param size 需要生成二維碼的大?。ǎ?
* @return bitmap
*/
public static Bitmap createQRCode(String text, int size) {
try {
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new QRCodeWriter().encode(text,
BarcodeFormat.QR_CODE, size, size, hints);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
pixels[y * size + x] = 0xffffffff;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
/**
* 生成帶logo的二維碼,默認二維碼的大小為500,logo為二維碼的1/5
*
* @param text 需要生成二維碼的文字、網(wǎng)址等
* @param mBitmap logo文件
* @return bitmap
*/
public static Bitmap createQRCodeWithLogo(String text, Bitmap mBitmap) {
return createQRCodeWithLogo(text,500,mBitmap);
}
/**
* 生成帶logo的二維碼,logo默認為二維碼的1/5
*
* @param text 需要生成二維碼的文字、網(wǎng)址等
* @param size 需要生成二維碼的大?。ǎ?
* @param mBitmap logo文件
* @return bitmap
*/
public static Bitmap createQRCodeWithLogo(String text, int size, Bitmap mBitmap) {
try {
IMAGE_HALFWIDTH = size/10;
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
/*
* 設(shè)置容錯級別,默認為ErrorCorrectionLevel.L
* 因為中間加入logo所以建議你把容錯級別調(diào)至H,否則可能會出現(xiàn)識別不了
*/
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix = new QRCodeWriter().encode(text,
BarcodeFormat.QR_CODE, size, size, hints);
int width = bitMatrix.getWidth();//矩陣高度
int height = bitMatrix.getHeight();//矩陣寬度
int halfW = width / 2;
int halfH = height / 2;
Matrix m = new Matrix();
float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
float sy = (float) 2 * IMAGE_HALFWIDTH
/ mBitmap.getHeight();
m.setScale(sx, sy);
//設(shè)置縮放信息
//將logo圖片按martix設(shè)置的信息縮放
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
mBitmap.getWidth(), mBitmap.getHeight(), m, false);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH
&& y > halfH - IMAGE_HALFWIDTH
&& y < halfH + IMAGE_HALFWIDTH) {
//該位置用于存放圖片信息
//記錄圖片每個像素信息
pixels[y * width + x] = mBitmap.getPixel(x - halfW
+ IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
} else {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
pixels[y * size + x] = 0xffffffff;
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
3、解析
生成二維碼,上面可以做的操作生成帶文字和網(wǎng)址、帶圖片的還有二維碼的大小,在代碼中都有了詳細的總結(jié)。
4、對二維碼進行放大和縮小的操作
public class MainActivity extends AppCompatActivity {
Bitmap bp = null;
ImageView imageview;
float scaleWidth;
float scaleHeight;
int h;
boolean num = false;
private Bitmap netfits;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics dm = new DisplayMetrics();//創(chuàng)建矩陣
getWindowManager().getDefaultDisplay().getMetrics(dm);
imageview = (ImageView) findViewById(R.id.imageview);
bp= BitmapFactory.decodeResource(getResources(),R.drawable.icon);
netfits = QRCode.createQRCodeWithLogo("張",200,bp);
int width = netfits.getWidth();
int height = netfits.getHeight();
int w = dm.widthPixels; //得到屏幕的寬度
final int h = dm.heightPixels; //得到屏幕的高度
scaleWidth = ((float) w) / width;
scaleHeight = ((float) w) / height;
imageview.setImageBitmap(netfits);
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (num == true) {
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBitmap = Bitmap.createBitmap(netfits, 0, 0, netfits.getWidth(), netfits.getHeight(), matrix, true);
imageview.setImageBitmap(newBitmap);
num = false;
} else {
Matrix matrix = new Matrix();
matrix.postScale(1.0f, 1.0f);
Bitmap newBitmap = Bitmap.createBitmap(netfits, 0, 0, netfits.getWidth(), netfits.getHeight(), matrix, true);
imageview.setImageBitmap(newBitmap);
num = true;
}
}
});
}
普通模式下我們可以自己處理:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: //當屏幕檢測到第一個觸點按下之后就會觸發(fā)到這個事件。
if (num == true) {
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBitmap = Bitmap.createBitmap(netfits, 0, 0, netfits.getWidth(), netfits.getHeight(), matrix, true);
imageview.setImageBitmap(newBitmap);
num = false;
} else {
Matrix matrix = new Matrix();
matrix.postScale(1.0f, 1.0f);
Bitmap newBitmap = Bitmap.createBitmap(netfits, 0, 0, netfits.getWidth(), netfits.getHeight(), matrix, true);
imageview.setImageBitmap(newBitmap);
num = true;
}
break;
}
return super.onTouchEvent(event);
}
添加功能:看是否選中,給二維碼添加圖片
private void initView() {
editText = (EditText) this.findViewById(R.id.qrcode_et);
button = (Button) this.findViewById(R.id.qrcode_bt);
imageView = (ImageView) this.findViewById(R.id.qrcode_iv);
cb = (CheckBox) this.findViewById(R.id.qrcode_cb);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (cb.isChecked()){
Bitmap bitmap = QRCode.createQRCodeWithLogo(editText.getText().toString(), 500,
BitmapFactory.decodeResource(getResources(),R.drawable.logo_me));
imageView.setImageBitmap(bitmap);
}else{
Bitmap bitmap = QRCode.createQRCode(editText.getText().toString(), 500);
imageView.setImageBitmap(bitmap);
}
}
});
}
上面是簡單的實現(xiàn)對二維碼的方法和縮小,如果更好的實現(xiàn),歡迎交流!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android之IphoneTreeView帶組指示器的ExpandableListView效果
在正在顯示的最上面的組的標簽位置添加一個和組視圖完全一樣的視圖,作為組標簽。這個標簽的位置要隨著列表的滑動不斷變化,以保持總是顯示在最上方,并且該消失的時候就消失2013-06-06
Android?APP瘦身shrinkResources使用問題詳解
這篇文章主要為大家介紹了Android?APP瘦身shrinkResources使用問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android自定義View實現(xiàn)內(nèi)存清理加速球效果
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)內(nèi)存清理加速球的效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
解析Android中實現(xiàn)滑動翻頁之ViewFlipper的使用詳解
有一些場景,我們需要向用戶展示一系列的頁面。比如我們正在開發(fā)一個看漫畫的應(yīng)用,可能就需要向用戶展示一張一張的漫畫圖片,用戶使用手指滑動屏幕,可以在前一幅漫畫和后一幅漫畫之間切換。這個時候ViewFlipper就是一個很好的選擇2013-05-05
Android使用AlertDialog創(chuàng)建對話框
這篇文章主要為大家詳細介紹了Android使用AlertDialog創(chuàng)建對話框的方法料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Linux系統(tǒng)下安裝android sdk的方法步驟
這篇文章主要介紹了Linux系統(tǒng)下安裝android sdk的方法步驟,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友可以們下面來一起看看吧。2017-03-03

