




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中怎么实现点击生成二维码功能
这期内容当中在下将会给大家带来有关Android中怎么实现点击生成二维码功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。QRCodeUtil:package
com.example.administrator.zxing;
import
android.graphics.Bitmap;
import
android.graphics.Canvas;
import
com.google.zxing.BarcodeFormat;
import
com.google.zxing.EncodeHintType;
import
com.google.zxing.WriterException;
import
mon.BitMatrix;
import
com.google.zxing.qrcode.QRCodeWriter;
import
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.util.HashMap;
import
java.util.Map;
public
class
QRCodeUtil
{
public
static
boolean
createQRImage(String
content,
int
widthPix,
int
heightPix,
Bitmap
logoBm,
String
filePath)
{
try
{
if
(content
==
null
||
"".equals(content))
{
return
false;
}
//配置参数
Map<EncodeHintType,
Object>
hints
=
new
HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,
"utf-8");
//容错级别
hints.put(EncodeHintType.ERROR_CORRECTION,
ErrorCorrectionLevel.H);
//设置空白边距的宽度
//
hints.put(EncodeHintType.MARGIN,
2);
//default
is
4
//
图像数据转换,使用了矩阵转换
BitMatrix
bitMatrix
=
new
QRCodeWriter().encode(content,
BarcodeFormat.QR_CODE,
widthPix,
heightPix,
hints);
int[]
pixels
=
new
int[widthPix
*
heightPix];
//
下面这里按照二维码的算法,逐个生成二维码的图片,
//
两个for循环是图片横列扫描的结果
for
(int
y
=
0;
y
<
heightPix;
y++)
{
for
(int
x
=
0;
x
<
widthPix;
x++)
{
if
(bitMatrix.get(x,
y))
{
pixels[y
*
widthPix
+
x]
=
0xff000000;
}
else
{
pixels[y
*
widthPix
+
x]
=
0xffffffff;
}
}
}
//
生成二维码图片的格式,使用ARGB_8888
Bitmap
bitmap
=
Bitmap.createBitmap(widthPix,
heightPix,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels,
0,
widthPix,
0,
0,
widthPix,
heightPix);
if
(logoBm
!=
null)
{
bitmap
=
addLogo(bitmap,
logoBm);
}
//必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
return
bitmap
!=
null
&&
press(Bitmap.CompressFormat.JPEG,
100,
new
FileOutputStream(filePath));
}
catch
(WriterException
|
IOException
e)
{
e.printStackTrace();
}
return
false;
}
/**
*
在二维码中间添加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;
}
}MainActivity:package
com.example.administrator.zxing;
import
android.content.Context;
import
android.graphics.BitmapFactory;
import
android.os.Bundle;
import
android.os.Environment;
import
android.support.v7.app.AppCompatActivity;
import
android.view.View;
import
android.widget.Button;
import
android.widget.CheckBox;
import
android.widget.EditText;
import
android.widget.ImageView;
import
java.io.File;
public
class
MainActivity
extends
AppCompatActivity
{
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//内容
final
EditText
contentET
=
(EditText)
findViewById(R.id.create_qr_content);
//显示二维码图片
final
ImageView
imageView
=
(ImageView)
findViewById(R.id.create_qr_iv);
//是否添加Logo
final
CheckBox
addLogoCB
=
(CheckBox)
findViewById(R.id.create_qr_addLogo);
Button
createQrBtn
=
(Button)
findViewById(R.id.create_qr_btn);
createQrBtn.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
v)
{
final
String
filePath
=
getFileRoot(MainActivity.this)
+
File.separator
+
"qr_"
+
System.currentTimeMillis()
+
".jpg";
//二维码图片较大时,生成图片、保存文件的时间可能较长,因此放在新线程中
new
Thread(new
Runnable()
{
@Override
public
void
run()
{
boolean
success
=
QRCodeUtil.createQRImage(contentET.getText().toString().trim(),
800,
800,
addLogoCB.isChecked()
?
BitmapFactory.decodeResource(getResources(),
R.mipmap.qr_logo)
:
null,
filePath);
if
(success)
{
runOnUiThread(new
Runnable()
{
@Override
public
void
run()
{
imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
}
});
}
}
}).start();
}
});
}
//文件存储根目录
private
String
getFileRoot(Context
context)
{
if
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File
external
=
context.getExternalFilesDir(null);
if
(external
!=
null)
{
return
external.getAbsolutePath();
}
}
return
context.getFilesDir().getAbsolutePath();
}
}布局:
activity_main:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.zxing.MainActivity">
<EditText
android:id="@+id/create_qr_content"
android:layout_width="match_parent"
android:layout_he
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 邵阳市重点中学2024-2025学年初三5月毕业班模拟考试数学试题含解析
- 江苏省盐城市响水实验、一中学2025届初三下学期零诊模拟生物试题含解析
- 廊坊卫生职业学院《成衣制作工艺》2023-2024学年第二学期期末试卷
- 江西师范大学科学技术学院《prote软件设计》2023-2024学年第二学期期末试卷
- 延寿县2025届数学四年级第二学期期末质量检测模拟试题含解析
- 天府新区航空旅游职业学院《欧美设计规范释义一双语》2023-2024学年第二学期期末试卷
- 天津石油职业技术学院《珠宝专业英语》2023-2024学年第二学期期末试卷
- 塔里木职业技术学院《测井资料解释课程设计》2023-2024学年第一学期期末试卷
- 辽宁税务高等专科学校《影像诊断学》2023-2024学年第二学期期末试卷
- 文山壮族苗族自治州马关县2024-2025学年数学三下期末综合测试模拟试题含解析
- 美国学生阅读技能训练
- 网络安全服务项目服务质量保障措施(实施方案)
- 生产加工型小微企业安全管理考试(含答案)
- 青少年科技创新比赛深度分析
- 世界近代武器革新图鉴(1722-1900)英国篇
- 安标受控件采购管理制度
- 亚低温的治疗与护理
- 危险化学品企业设备完整性 第2部分 技术实施指南 编制说明
- 防高坠自查自纠台账
- GB/T 4437.1-2023铝及铝合金热挤压管第1部分:无缝圆管
- 市政工程消耗量定额 zya1-31-2015
评论
0/150
提交评论