bitmap+文本生成新的bitmap的实现
注:参数content为生成二维码bitmap的内容,该二维码bitmap在和文本title组合生成一个新的bitmap
package info.ecloud.merchant.util; import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.journeyapps.barcodescanner.BarcodeEncoder; import java.util.Hashtable; public class SaveImageUtils { private static final String CHARSET = "UTF-8";
/**
* 生成图片 加上title的图片
* @param content
* @param title
* @return
*/
public static Bitmap createImage(String content,String title) {
int picWidth = ;//生成图片的宽度
int picHeight = ;//生成图片的高度
int titleTextSize = ;
int textColor = Color.BLACK;
int qrWidth = ;
int qrHeight = ;
int paddingTop = ;
int paddingMiddle = ;
int paddingBottom = ; //最终生成的图片
Bitmap result = Bitmap.createBitmap(picWidth,picHeight,Bitmap.Config.ARGB_8888); Paint paint = new Paint();
paint.setColor(Color.WHITE);
Canvas canvas = new Canvas(result); //先画一整块白色矩形块
canvas.drawRect(,,picWidth,picHeight,paint); //画title文字
Rect bounds = new Rect();
paint.setColor(textColor);
paint.setTextSize(titleTextSize);
//获取文字的字宽高,以便将文字与图片中心对齐
paint.getTextBounds(title,,title.length(),bounds);
//画白色矩形块
int qrTop = paddingTop+titleTextSize+paddingMiddle;//二维码的顶部高度 //画二维码
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, );
Bitmap image = null;
try {
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
image = barcodeEncoder.encodeBitmap((String) content, BarcodeFormat.QR_CODE, , );
} catch (Exception e) {
e.printStackTrace();
} paint.setColor(Color.BLACK);
canvas.drawBitmap(image,(picWidth-qrWidth)/,qrTop,paint); //画文字
paint.setColor(Color.BLACK);
paint.setTextSize(titleTextSize);
int line = ;
int textTop = qrTop+qrHeight+paddingBottom;//地址的顶部高度 for (int i = ; i < line ; i++){
String[] split = title.split("-");
paint.getTextBounds(title,,split[i].length(),bounds);
canvas.drawText(split[i],picWidth/-bounds.width()/,textTop+i*titleTextSize+i*+bounds.height()/,paint);
} canvas.save();
canvas.restore(); return result;
} }
bitmap+文本生成新的bitmap的实现的更多相关文章
- android Bitmap(将视图转为bitmap对象)
1)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap main.xml文件如下: <?xmlversion="1.0"encodi ...
- C#图片裁切,生成新图片
/// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png 新图1_new.png /// </summary> /// <param name="pic ...
- Texygen文本生成,交大计算机系14级的朱耀明
文本生成哪家强?上交大提出基准测试新平台 Texygen 2018-02-12 13:11测评 新智元报道 来源:arxiv 编译:Marvin [新智元导读]上海交通大学.伦敦大学学院朱耀明, 卢思 ...
- Android从本地选择图片文件转为Bitmap,并用zxing解析Bitmap
如何从本地选择图片文件 使用Intent调用系统相册后,onActivityResult函数返回的是Uri格式的路径 /** * 打开系统相册 */ private void openSysAlbum ...
- phpword读取内容和样式 生成新的内容
table样式还未读出 正在测试中, 目前有 rows cell textrun等样式 顺序不固定 可以设定 <?php require 'vendor/autoload.php'; use P ...
- 斯坦福NLP课程 | 第15讲 - NLP文本生成任务
作者:韩信子@ShowMeAI,路遥@ShowMeAI,奇异果@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www. ...
- 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]
原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...
- IOS 截取图片 部分 并生成新图片
/** * 从图片中按指定的位置大小截取图片的一部分 * * @param image UIImage image 原始的图片 * @param rect CGRect rect 要截取的区域 * * ...
- 根据backup-my.cnf来生成新的事务日志文件
使用rpm包安装xtrabackup ## 安装依赖包yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-D ...
随机推荐
- shh登入不能自动执行.bashrc
在linux 上新安装的anconda来管理软件,把环境变量放在home目录的.bashrc.结果每次开终端,不能直接使用conda.需要手动加环境变量. 用户登入后计算机执行了哪些文件 用户登录时b ...
- 管道模式 pipe
先放一个图,预则立嘛
- Halo-个人独立博客系统
项目地址:https://github.com/halo-dev/halo 安装指导:https://halo.run/guide/ 简介: Halo 是一款现代化的个人独立博客系统,给习惯写博客 ...
- larabel Artisan Command 使用总结
larabel Artisan Command 使用总结 定义命令 在routes/console.php下定义命令 Artisan::command('ltf', function () { (ne ...
- Linux 6种日志查看方法,不会看日志会被鄙视的
作为一名后端程序员,和Linux打交道的地方很多,不会看Linux日志,非常容易受到来自同事和面试官的嘲讽,所以掌握一种或者几种查看日志的方法非常重要. Linux查看日志的命令有多种: tail.c ...
- MySQL Router单点隐患通过Keepalived实现
目录 一.介绍 二.环境准备 三.安装步骤 3.1下载软件包,解压 3.2源码安装 3.3配置keepalived 3.4修改keepalived配置文件 3.5启动keepalived 3.6查看V ...
- Springboot | Failed to execute goal org.springframework.boot:spring-boot-maven-plugin
案例 今天搭建spring boot 环境时,使用mvn install ,出现Failed to execute goal org.springframework.boot:spring-boot- ...
- 为什么不在spring容器管理controller
Spring容器与SpringMVC容器 1.疑问:为什么不用spring去管理所有类? 我们配置springMVC 中,为什么controller不直接交给spring 管理而要spring MVC ...
- CUDA学习(三)之使用GPU进行两个数组相加
传入两个数组,在GPU中将两个数组对应索引位置相加 #include "cuda_runtime.h" #include "device_launch_parameter ...
- 物流跟踪API-快递单推送
上一篇文章我们讲解了订阅服务功能.我们已经完成了如何把物流订单订阅到快递鸟,快递鸟也能接收到我们的订单信息,接下来就需要快递鸟实时的将最新的物流轨迹推送到我们服务器,我们既然要接收快递鸟的信息,就需要 ...