1.导入 zxinglib包
//扫描按钮
public void
scannner
(View view){
Intent openCamera =
new
Intent(
this,
CaptureActivity.
class
)
;
startActivityForResult(openCamera
,
0
)
;
}
//生成二维码按钮
public void
gener_pic
(View view){
String contentString =
"22222"
;
if
(!contentString.equals(
""
)) {
//根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350)
Bitmap qrCodeBitmap =
null;
try
{
qrCodeBitmap = BitmapUtils.
create2DCode
(contentString)
;
}
catch
(WriterException e) {
e.printStackTrace()
;
}
gen_id
.setImageBitmap(qrCodeBitmap)
;
}
}
//扫描回传
@Override
protected void
onActivityResult
(
int
requestCode
, int
resultCode
,
Intent data) {
super
.onActivityResult(requestCode
,
resultCode
,
data)
;
if
(resultCode==
RESULT_OK
){
Bundle bundle = data.getExtras()
;
String result = bundle.getString(
"result"
)
;
result_button_view
.setText(result)
;
// gen_id.setImageBitmap((Bitmap) data.getParcelableExtra("bitmap"));
}
// 通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels
,
0
,
width
,
0
,
0
,
width
,
height)
;
下面添加:
if
(logoBm !=
null
) {
bitmap =
addLogo
(bitmap
,
logoBm)
;
}
在BitmapUtils中添加方法
/**
* 在二维码中间添加Logo图案
*/
private static
Bitmap
addLogo
(Bitmap src
,
Bitmap logo) {
if
(src ==
null
) {
return null;
}
if
(logo ==
null
) {
return
src
;
}
//获取图片的宽高
int
srcWidth = src.getWidth()
;
int
srcHeight = src.getHeight()
;
int
logoWidth = logo.getWidth()
;
int
logoHeight = logo.getHeight()
;
if
(srcWidth ==
0
|| srcHeight ==
0
) {
return null;
}
if
(logoWidth ==
0
|| logoHeight ==
0
) {
return
src
;
}
//logo大小为二维码整体大小的1/5
float
scaleFactor = srcWidth *
1.0f
/
5
/ logoWidth
;
Bitmap bitmap = Bitmap.
createBitmap
(srcWidth
,
srcHeight
,
Bitmap.Config.
ARGB_8888
)
;
try
{
Canvas canvas =
new
Canvas(bitmap)
;
canvas.drawBitmap(src
,
0
,
0
, null
)
;
canvas.scale(scaleFactor
,
scaleFactor
,
srcWidth /
2
,
srcHeight /
2
)
;
canvas.drawBitmap(logo
,
(srcWidth - logoWidth) /
2
,
(srcHeight - logoHeight) /
2
, null
)
;
canvas.save(Canvas.
ALL_SAVE_FLAG
)
;
canvas.restore()
;
}
catch
(Exception e) {
bitmap =
null;
e.getStackTrace()
;
}
return
bitmap
;
}