Android 点击生成二维码功能实现代码
作者:美女婕 发布时间:2022-08-26 18:15:23
标签:android,二维码
先看效果:
输入内容,点击生成二维码:
点击logo图案:
代码:
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 com.google.zxing.common.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 && bitmap.compress(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="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/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_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/create_qr_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生成二维码" />
<CheckBox
android:id="@+id/create_qr_addLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加logo图案" />
</LinearLayout>
<ImageView
android:id="@+id/create_qr_iv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
总结
以上所述是小编给大家介绍的Android 点击生成二维码功能实现代码网站的支持!
来源:http://blog.csdn.net/qq_38913065/article/details/78488729


猜你喜欢
- 这篇文章主要介绍了通过实例解析JMM和Volatile底层原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 简介switch的新特性可是源远流长,早在JDK 12就以预览功能被引入了,最终在JDK 14成为了正式版本的功能:JEP 361: Swi
- 1、导入资源2、JSP代码<div class="page-container">  
- 本文实例讲述了java读取properties文件的方法。分享给大家供大家参考。具体实现方法如下:package com.test.demo
- 前言前面的篇幅里有提到通过InitializingBean和Disposable等接口可以对bean的初始化和销毁做一些自定义操作,那么有一
- 前言上一篇讲了普通轮询、加权轮询的两种实现方式,重点讲了平滑加权轮询算法,并在文末留下了悬念:节点出现分配失败时降低有效权重值;成功时提高有
- public class TestSqlserverJtds { public static void main(String[]
- 本文实例汇总了C#中@的用法,对C#程序设计来说有不错的借鉴价值。具体如下:一 字符串中的用法1.学过C#的人都知道C# 中字符串常量可以以
- 引言内存管理一直是JAVA语言自豪与骄傲的资本,它让JAVA程序员基本上可以彻底忽略与内存管理相关的细节,只专注于业务逻辑。不过世界上不存在
- 1、购买或本地生成ssl证书要使用https,首先需要证书,获取证书的两种方式:1、自己通过keytool生成2、通过证书授权机构购买###
- 概述状态模式允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。这个模式将状态封装成独立的类,并将动作委托到 代表当前状态的对
- 场景做为一个码农,大部分都集中在一二线城市,所以租房也就无可避免,面对如今五花八门的租房信息,往往很难找到合适的房子。而如今的这些租房软件,
- 要判断输入金额为正确金额的方法有两个,一个是用正则表达式,另一个就是用textfield的代理方法有时候难免遇到这样的需求,不符合规则的金额
- 基本操作示例VectorApp.javaimport java.util.Vector; import java.lang.*; impor
- GUI编程实现贪吃蛇小游戏,供大家参考,具体内容如下1、编写主方法实现启动类2、准备好素材图片,编写数据类3、代码主体部分:在panel面板
- 本文实例为大家分享了java实现计算器功能具体代码,供大家参考,具体内容如下效果图组成结构从结构上来说,一个简单的图形界面,需要由界面组件、
- 本文所述实例实现WinForm自定义函数FindControl实现按名称查找控件的功能,在C#程序开发中有一定的实用价值。分享给大家供大家参
- 1.点 “window”-> "Preferences" -> "Java" ->
- 这篇文章主要介绍了深入了解JVM字节码增强技术,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- 本文实例讲述了C#之Expression表达式树,分享给大家供大家参考。具体实现方法如下:表达式树表示树状数据结构的代码,树状结构中的每个节