一、問題描述
在開發中需要將信息轉換為二維碼存儲並要求帶有公司的logo,我們知道Google的Zxing開源項目就很好的幫助我們實現條形碼、二維碼的生成和解析,但帶有logo的官網並沒有提供demo,下面就通過實例看看如何實現以及Zxing的使用。
二、案例介紹
1、案例運行效果

2、案例准備工作
在項目中加入jar,只需加入core.jar
Zxing項目地址:https://github.com/zxing/zxing/
三、Zxing主要組件
1、BarcodeFormat
定義了不同的二進制編碼方式,取值如下
EAN_13條形碼,共計13位代碼,比較常見,如商品上的包裝上的都是這種條形碼
CODE_QR二維碼(矩陣碼),比條形碼存在更多信息,當下比較流行
CODE_128條形碼 可表示可表示從 ASCII 0 到ASCII 127 共128個 字符 ,用於企業管理,生產流程控制
CODE_39條形碼,編制簡單只接受如下43個字符
2、MultiFormatWriter
主要包含一個 encode()方法,可實現產生編碼(條形、二維碼)
BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Hashtable hints)方法
參數 :
contents:要編碼的內容
format:編碼格式(條形、二維)
width,height:生成碼的大小
hints:包含EncodeHintType(編碼提示類型)信息的集合,主要設置字符編碼,比如支持漢字的utf-8,如下:
Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>();
hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");
返回值 :BitMatrix 二維矩陣點
3、BitMatrix
BitMatrix :表現為一個二維矩陣,x表示列的位置,y表示行的位置,循序從左上角開始,一列一列排列(先x后y)
主要方法 :
getWidth():返回矩陣的寬度
getHeight():返回矩陣的高度
boolean get(x,y) :非常重要的方法,實現根據給定的x,y判斷該位置是否有黑塊
在產生二維碼的應用中就是通過這個方法進行判斷,然后把有黑塊的點記錄下來,使用Bitmap的setPixels()方法生成圖形,詳解案例的createCode()方法中的代碼
四、完整代碼
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
public
class
MainActivity
extends
Activity {
private
EditText etCompany;
private
EditText etPhone;
private
EditText etEmail;
private
EditText etWeb;
private
Bitmap logo;
private
static
final
int
IMAGE_HALFWIDTH =
40
;
//寬度值,影響中間圖片大小
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲得資源圖片,可改成獲取本地圖片或拍照獲取圖片
logo=BitmapFactory.decodeResource(
super
.getResources(),R.drawable.y014);
etCompany =(EditText) findViewById(R.id.etCompany);
etPhone=(EditText) findViewById(R.id.etPhone);
etEmail =(EditText) findViewById(R.id.etEmail);
etWeb =(EditText) findViewById(R.id.etWeb);
findViewById(R.id.but).setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
String company=etCompany.getText().toString().trim() ;
String phone =etPhone .getText().toString().trim() ;
String email = etEmail.getText().toString().trim() ;
String web = etWeb.getText().toString().trim() ;
//二維碼中包含的文本信息
String contents=
"BEGIN:VCARD\nVERSION:3.0\nORG:"
+company+
"\nTEL:"
+phone+
"\nURL:"
+web+
"\nEMAIL:"
+email+
"\nEND:VCARD"
;
try
{
//調用方法createCode生成二維碼
Bitmap bm=createCode(contents,logo,BarcodeFormat.QR_CODE);
ImageView img=(ImageView)findViewById(R.id.imgCode) ;
//將二維碼在界面中顯示
img.setImageBitmap(bm);
}
catch
(WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/**
* 生成二維碼
* @param string 二維碼中包含的文本信息
* @param mBitmap logo圖片
* @param format 編碼格式
* [url=home.php?mod=space&uid=309376]@return[/url] Bitmap 位圖
* @throws WriterException
*/
public
Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format)
throws
WriterException {
Matrix m =
new
Matrix();
float
sx = (
float
)
2
* IMAGE_HALFWIDTH / mBitmap.getWidth();
float
sy = (
float
)
2
* IMAGE_HALFWIDTH
/ mBitmap.getHeight();
m.setScale(sx, sy);
//設置縮放信息
//將logo圖片按martix設置的信息縮放
mBitmap = Bitmap.createBitmap(mBitmap,
0
,
0
,
mBitmap.getWidth(), mBitmap.getHeight(), m,
false
);
MultiFormatWriter writer =
new
MultiFormatWriter();
Hashtable<EncodeHintType, String> hst =
new
Hashtable<EncodeHintType, String>();
hst.put(EncodeHintType.CHARACTER_SET,
"UTF-8"
);
//設置字符編碼
BitMatrix matrix = writer.encode(string, format,
400
,
400
, hst);
//生成二維碼矩陣信息
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++) {
//迭代列
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
(matrix.get(x, y)) {
//如果有黑塊點,記錄信息
pixels[y * width + x] =
0xff000000
;
//記錄黑塊信息
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通過像素數組生成bitmap
bitmap.setPixels(pixels,
0
, width,
0
,
0
, width, height);
return
bitmap;
}
}
|
