Android 生成二维码,条形码,二维码添加logo

zxing生成二维码

implementation 'com.google.zxing:core:3.3.1'
implementation(name: 'zxing-1.0.1', ext: 'aar')
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

 

    private Bitmap getCodeBitmap(String content, int logoId) {BitmapFactory.Options opt = new BitmapFactory.Options();opt.inPreferredConfig = Bitmap.Config.RGB_565;opt.inPurgeable = true;opt.inInputShareable = true;// 获取资源图片
//        @SuppressLint("ResourceType") InputStream is = getResources().openRawResource(logoId);
//        Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(content, (int) getResources().getDimension(R.dimen.normal_200dp), getResources().getColor(R.color.color_333), null/*BitmapFactory.decodeStream(is, null, opt)*/);
//        Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(content, (int) getResources().getDimension(R.dimen.normal_200dp), getResources().getColor(R.color.color_333), BitmapFactory.decodeStream(is, null, opt));Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(content, (int) getResources().getDimension(R.dimen.normal_290dp), getResources().getColor(R.color.color_333));return bitmap;}
Glide.with(gsewmimg).load(getCodeBitmap(response.data.skip, R.mipmap.zhifuicon)).into(gsewmimg);
//gsewmimg.setImageBitmap(getCodeBitmap(response.data.skip, R.mipmap.zhifuicon));

方法二:

添加zxing依赖:

compile 'cn.yipianfengye.android:zxing-library:2.2'
Bitmap mBitmap = CodeUtils.createImage("sssss77777", 400, 400, null);//无logo
mBitmap = CodeUtils.createImage("ssss6666", 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));//logo
imageView.setImageBitmap(mBitmap);

 

方法一:

zxing.jar下载地址:https://pan.baidu.com/s/1_XLBnj7daO6bzP-tlRwKzg

下载下来复制到libs文件夹下,并Add As Library

 

<ImageViewandroid:id="@+id/iv_qrcode"android:scaleType="fitXY"android:layout_width="150dp"android:layout_height="150dp"/>

 

        ImageView ivQrCode = (ImageView)findViewById(R.id.iv_qrcode);Bitmap bitmap = strTo2DCode("aabbbb", 800, 800);ivQrCode.setImageBitmap(addLogo(bitmap,BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)));//有logo//        ivQrCode.setImageBitmap(bitmap);//无logo

 

 

1、生成二维码方法:

/*** 字符串生成二维码** @param strEncode 要生成二维码的字符串* @param width     要生成图片的宽度* @param height    要生成图片的高度* @return*/
public static Bitmap strTo2DCode(String strEncode, int width, int height) {Bitmap bitmap = null;try {BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(strEncode.getBytes("UTF-8"), "iso-8859-1"), BarcodeFormat.QR_CODE, width, height);// 二维矩阵转为一维像素数组,也就是一直横着排了int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {if (bitMatrix.get(x, y)) {pixels[y * width + x] = 0xff000000;} else {pixels[y * width + x] = 0x00000000;}}}bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 通过像素数组生成bitmap,具体参考apibitmap.setPixels(pixels, 0, width, 0, 0, width, height);} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}return bitmap;
}

2、向二维码中心添加logo方法:

/*** 在二维码中间添加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/5float 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.restore();} catch (Exception e) {bitmap = null;e.getStackTrace();}return bitmap;
}

 

3、条形码

添加依赖

implementation "com.google.zxing:core:3.3.1"

工具方法

public class CodeUtils {/*** 生成条形码(不支持中文)** @param content* @return*/public static Bitmap createBarcode(String content) {try {BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, 3000, 700);int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {pixels[offset + x] = bitMatrix.get(x, y) ? 0xff000000 : 0xFFFFFFFF;}}Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;} catch (WriterException e) {e.printStackTrace();}return null;}/*** 生成二维码** @param content* @return*/public static Bitmap createQrcode(String content) {Map<EncodeHintType, Object> hints = new HashMap<>();// 支持中文配置hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);try {BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 1000, 1000, hints);int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {pixels[offset + x] = bitMatrix.get(x, y) ? 0xff000000 : 0xFFFFFFFF;}}Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;} catch (WriterException e) {e.printStackTrace();}return null;}}

调用

ImageView ivBarcode = findViewById(R.id.iv_barcode);
ImageView ivQrcode = findViewById(R.id.iv_qrcode);
ivBarcode.setImageBitmap(CodeUtils.createBarcode("This is a barcode"));//条形码
ivQrcode.setImageBitmap(CodeUtils.createQrcode("This is a qrcode"));//二维码

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415191.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java setDataSource 报红

开始学习spring security遇到一个问题&#xff0c;setDataSource老是报红 解决方案&#xff0c;在pom.xml中增加 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId><version>2…

Android 识别图片二维码,以及设置状态栏颜色

zxing依赖&#xff1a;compile cn.yipianfengye.android:zxing-library:2.2 初始化&#xff1a;private String SAVE_PIC_PATH Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)? Environment.getExternalStorageDirectory().getAbsolute…

linux之sed

sed 是一个流编辑器(stream editor)&#xff0c;主要用来执行文本替换。但 sed 的主要设计目的是以批处理的方式而不是交互的方式来编辑文件。 命令简介 基本命令格式 sed [常用选项] 命令文本 输入 常用选项 -n (--quiet, --silent)&#xff1a;安静模式。在 sed 的基本用法中…

mac 升级 15.4之后,部分软件无法打开解决

我的mac升级版本后&#xff0c;有一些好用的软件就打不开了&#xff0c;比如orc识别软件text scanner 解决方法&#xff1a; sudo codesign -f -s - --deep /Applications/TextScan.app可以直接输入 sudo codesign -f -s - --deep 再把文件拖拽过去即可

Android 截图,截取指定view截图

二、具体实现方式 实用截图方法截取整个activity public static Bitmap shotActivity(Activity ctx) {View view ctx.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap bp Bitmap.createBitmap(view.getDrawingCache(), 0, 0…

java使用AntPathMatcher进行uri匹配

需求&#xff1a;我在做rbac权限校验的时候&#xff0c;设置管理员的访问路径为/admin/**,希望所有的开头为/admin/的uri操作地址都能进行匹配判断。 import org.springframework.util.AntPathMatcher;String content "/admin/acuff"; String pattern "/admi…

百度经验 回享计划

https://jingyan.baidu.com/user/income 转载于:https://www.cnblogs.com/qdrs/p/7940353.html

Android 扫描二维码demo

demo下载链接&#xff1a;https://download.csdn.net/download/meixi_android/10779714 zxing依赖&#xff1a; compile cn.yipianfengye.android:zxing-library:2.2 扫描类&#xff1a; /*** 作者&#xff1a;created by meixi* 邮箱&#xff1a;13164716840163.com* 日期&…

neditor 自定义工具栏配置

neditor是ueditor的亚种。我已经实现了vueneditor的封装&#xff0c;下面记录一下常用工具栏的配置。 1、配置方法 实例化编辑器的时候传入 toolbars 参数 2、参数列表 名称描述anchor锚点undo撤销redo重做bold加粗indent首行缩进snapscreen截图italic斜体underline下划线st…

前端学习(2666):完成vue3.0的todolist编辑

1点击编辑 2编辑逻辑 3进入编辑状态

课后作业-团队编程项目总结

成员&#xff1a;王志昂&#xff08;组长&#xff09; 郑 月 李古宇 孙晨旭 鞠牧孜 程冠菲 项目名称&#xff1a;吃货之家 早在商朝末年&#xff0c;太公望(别名:姜太公&#xff0c;姜子牙)在营丘之战时所创制的太公望红焖鸡&#xff0c;在姜太公建立齐国后&#xff0c;红焖鸡(…

Android Notification 手机系统横幅弹出提示框调用,横幅通知,RemoteViews使用实例

直接上代码 &#xff1a;bundle是极光推送的bundle Override public void onReceive(Context context, Intent intent) {try {Bundle bundle intent.getExtras(); 。。。。。。 发送横幅通知方法&#xff1a; try {RemoteViews customView new RemoteViews(context.getPac…

前端学习(2667):退出编辑状态

1逻辑实现 2加上myinput 3判断myinput 调整元素 4

《构建之法》阅读笔记01

本周阅读了《构建之法》的第一章《概论》以及第四章《两人合作》。 《概论》旨在说明软件工程的概念。 作为一个程序员&#xff0c;几乎没有谁不知道“程序算法数据结构”这句名言的&#xff0c;而这本书中&#xff0c;则又提出了另一个概念“软件程序软件工程”&#xff0c;“…

Android 帧动画,加载动画,AnimationDrawable,仿京东加载动画

1、创建drawable文件ring_animation.xml <?xml version"1.0" encoding"utf-8"?> <animation-list xmlns:android"http://schemas.android.com/apk/res/android"><item android:drawable"mipmap/windmill_1"android:…

前端学习(2668):删除功能

1定义删除 2删除方法 3删除演示