/***   生成条形码和二维码的工具*/
public class ZXingUtils {/*** 生成二维码 要转换的地址或字符串,可以是中文** @param url* @param width* @param height* @return*/public static Bitmap createQRImage(String url, final int width, final int height) {try {// 判断URL合法性if (url == null || "".equals(url) || url.length() < 1) {return null;}Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//容错率//L 7,M 15,Q 25,H 30.hints.put(EncodeHintType.ERROR_CORRECTION, "H");//边距 0最小 1   2最大hints.put(EncodeHintType.MARGIN, "1");// 图像数据转换,使用了矩阵转换BitMatrix bitMatrix = new QRCodeWriter().encode(url,BarcodeFormat.QR_CODE, width, height, hints);int[] pixels = new int[width * height];// 下面这里按照二维码的算法,逐个生成二维码的图片,// 两个for循环是图片横列扫描的结果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] = 0xffffffff;}}}// 生成二维码图片的格式,使用ARGB_8888Bitmap 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;}/*** 用于向创建的二维码中添加一个logo* @param bm_QR* @param bm_logo* @return*/public static Bitmap addLogo(Bitmap bm_QR, Bitmap bm_logo) {if (bm_QR == null) {return null;}if (bm_logo == null) {return bm_QR ;}//获取图片的宽高int bm_QR_Width = bm_QR.getWidth() ;int bm_QR_Height = bm_QR.getHeight();int bm_logo_Width = bm_logo.getWidth() ;int bm_logo_Height = bm_logo.getHeight();//设置log0的大小为二维码整体大小的1/4float scale_logo = bm_QR_Width*1.0f /4/bm_logo_Width ;Bitmap bitmap = Bitmap.createBitmap(bm_QR_Width, bm_QR_Height, Bitmap.Config.ARGB_8888);try {Canvas canvas = new Canvas(bitmap);canvas.drawBitmap(bm_QR, 0, 0, null);canvas.scale(scale_logo, scale_logo, bm_QR_Width / 2, bm_QR_Height / 2);canvas.drawBitmap(bm_logo, (bm_QR_Width - bm_logo_Width) / 2, (bm_QR_Height - bm_logo_Height) / 2, null);canvas.save();canvas.restore();} catch (Exception e) {bitmap = null;e.getStackTrace();}return bitmap;}/*** 生成条形码** @param context* @param contents*            需要生成的内容* @param desiredWidth*            生成条形码的宽带* @param desiredHeight*            生成条形码的高度* @param displayCode*            是否在条形码下方显示内容* @return*/public static Bitmap creatBarcode(Context context, String contents,int desiredWidth, int desiredHeight, boolean displayCode) {Bitmap ruseltBitmap = null;/*** 图片两端所保留的空白的宽度*/int marginW = 20;/*** 条形码的编码类型*/BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;if (displayCode) {Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat,desiredWidth, desiredHeight);Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth + 2* marginW, desiredHeight, context);ruseltBitmap = mixtureBitmap(barcodeBitmap, codeBitmap, new PointF(0, desiredHeight));} else {ruseltBitmap = encodeAsBitmap(contents, barcodeFormat,desiredWidth, desiredHeight);}return ruseltBitmap;}/*** 生成条形码的Bitmap** @param contents*            需要生成的内容* @param format*            编码格式* @param desiredWidth* @param desiredHeight* @return* @throws WriterException*/protected static Bitmap encodeAsBitmap(String contents,BarcodeFormat format, int desiredWidth, int desiredHeight) {final int WHITE = 0xFFFFFFFF;final int BLACK = 0xFF000000;MultiFormatWriter writer = new MultiFormatWriter();BitMatrix result = null;try {result = writer.encode(contents, format, desiredWidth,desiredHeight, null);} catch (WriterException e) {// TODO Auto-generated catch blocke.printStackTrace();}int width = result.getWidth();int height = result.getHeight();int[] pixels = new int[width * height];// All are 0, or black, by defaultfor (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;}}Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}/*** 生成显示编码的Bitmap** @param contents* @param width* @param height* @param context* @return*/protected static Bitmap creatCodeBitmap(String contents, int width,int height, Context context) {TextView tv = new TextView(context);ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);tv.setLayoutParams(layoutParams);tv.setText(contents);tv.setHeight(height);tv.setGravity(Gravity.CENTER_HORIZONTAL);tv.setWidth(width);tv.setDrawingCacheEnabled(true);tv.setTextColor(Color.BLACK);tv.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());tv.buildDrawingCache();Bitmap bitmapCode = tv.getDrawingCache();return bitmapCode;}/*** 将两个Bitmap合并成一个** @param first* @param second* @param fromPoint*            第二个Bitmap开始绘制的起始位置(相对于第一个Bitmap)* @return*/protected static Bitmap mixtureBitmap(Bitmap first, Bitmap second,PointF fromPoint) {if (first == null || second == null || fromPoint == null) {return null;}int marginW = 20;Bitmap newBitmap = Bitmap.createBitmap(first.getWidth() + second.getWidth() + marginW,first.getHeight() + second.getHeight(), Bitmap.Config.ARGB_4444);Canvas cv = new Canvas(newBitmap);cv.drawBitmap(first, marginW, 0, null);cv.drawBitmap(second, fromPoint.x, fromPoint.y, null);cv.save();cv.restore();return newBitmap;}}

Android 生成二维码工具类相关推荐

  1. java将链接生成二维码工具类

    一.添加依赖 <!-- 生成二维码--><dependency><groupId>com.google.zxing</groupId><artif ...

  2. 分享一个Java生成二维码工具类

    分享一个Java生成二维码工具类 直接上代码: 1.CodeUtil.class package top.lrshuai.blog.util;import java.awt.BasicStroke; ...

  3. java生成二维码工具类

    1,先引入谷歌的插件zxing的maven坐标 <!-- 二维码 --><dependency><groupId>com.google.zxing</grou ...

  4. 2021-08-26(条形码,二维码工具类生成)

    公司需要对商品的条码值生成对应的条形码,目前市面上的有的条码库分为: JBarcode,zxing jbarcode这个包在maven的官方仓库找不到,所以需要自己添加到本地仓库然后编写pom文件就可 ...

  5. Java生成和解析二维码工具类(简单经典)

    Java生成和解析二维码工具类 开箱即用,简单不废话. pom.xml引入依赖 <!-- https://mvnrepository.com/artifact/com.google.zxing/ ...

  6. Android 生成二维码

    二维码的应用非常广泛,用途也很多,网上也有很多实用的精简包可以引入,本篇主要记录了不用引入新的module,自己集成生成二维码功能的过程. 一.引入依赖 首先在libs文件目录下放进jar包zxing ...

  7. Java二维码工具类(使用zxing实现,可支持logo)

    本工具类基于Google二维码框架zxing3.1.0实现. 1. pom.xml中依赖包设置 <dependency><groupId>com.google.zxing< ...

  8. com.google.zxing 二维码工具类

    com.google.zxing 二维码工具类 pom 工具类 使用 pom <dependency><groupId>com.google.zxing</groupId ...

  9. Java二维码工具类(超详细注释)

    二维码工具类 准备工作: pom.xml 引入依赖 <!-- 二维码 --> <dependency><groupId>com.google.zxing</g ...

最新文章

  1. ICPC 2019国际大学生程序设计竞赛,中国高校未能夺冠
  2. java basedaoimpl_java web项目DAO层通用接口BaseDao与实现类BaseDaoImpl
  3. MongoDB的学习--聚合
  4. halcon深度学习算子,持续更新
  5. Linux学习笔记——gzip命令
  6. java jna jni_JNA, Java Native开发利器
  7. 机器人总动员最后的bgm_引导你欣赏《机器人瓦力》的电影配乐
  8. LeetCode 1801. 积压订单中的订单总数(map)
  9. 华为手机如何固定横屏_华为手机如何录屏?原来方法这么简单,手把手教你学会...
  10. java test 用法,pytest基本用法简介
  11. python能做什么工作-学完Python我们可以做什么工作?
  12. 服务器自动几点重启php,windows下apache及mysql定时自动重启服务器定时自动重启...
  13. linux中的bg命令作用,linux bg和fg命令
  14. Chrome浏览器修改繁体为中文简体
  15. 淘宝用户行为分析——用户画像
  16. 一键实现证件照背景的替换,Python 制作可视化GUI界面真香啊
  17. python怎么编辑浏览器_怎样修改anaconda默认浏览器
  18. uniapp 离线安卓本地打包(利用保利威视的打包工程打包)
  19. IE6、IE7、IE8之IE多版本共存的几种方法(转)
  20. 计算机程序占用端口,程序启动发现端口被占?3步查出它是谁!

热门文章

  1. 清华学计算机的住在哪个公寓,清华大学周边住宿攻略_清华大学附近住哪里好?...
  2. python用七巧板图片画个图_canvas 入门-利用 canvas 制作一个七巧板
  3. 【opencv】目标跟踪之MOSSE算法配合模板匹配实现初始滤波器的自动初始化
  4. GPRS连接阿里云物联网平台四
  5. Linux下暴力破解弱密码的工具
  6. woocommerce 分类到菜单_我如何为每个WooCommerce产品类别创建不同的菜单?
  7. 整理UML建模概念和图形~(啥?程序员不再写代码,变成画图工程师?)
  8. Shell命令-文件及目录操作之pwd、rm
  9. 显卡驱动的作用(本质作用)
  10. [Android实例] [版主原创]ScrollView嵌套ScrollView