/**
* @param filePath
* 源图片路径
* @param markContent
* 图片中添加内容
* @param outPath
* 输出图片路径 字体颜色等在函数内部实现的
*
* @param
*/
// 给jpg添加文字
public boolean createStringMark(String url , String filePath, String markContent, int R,int G,int B,String choosefont) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null) == -1 ? 200 : theImg.getWidth(null);
int height = theImg.getHeight(null) == -1 ? 200 : theImg.getHeight(null);
System.out.println(width);
System.out.println(height);
System.out.println(theImg);
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics(); //Color mycolor1 = Color.white;
Color mycolor1 = new Color(255,255,255);
g.setColor(mycolor1);
g.setBackground(Color.red);
g.drawImage(theImg, 0, 0, null); // 方法在画布上绘制图像、画布或视频。
//Font font=new Font("黑体", Font.BOLD, 200);
Font font=new Font(choosefont, Font.BOLD, 200);
font = Font(200);
g.setFont(font); // 字体、字型、字号 三个字350
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 计算文字长度,计算居中的x点坐标
FontMetrics fm = g.getFontMetrics(font);
int textWidth = fm.stringWidth(markContent);
int widthX = (width - textWidth) / 2;
// 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
g.drawString(markContent,widthX,1130); Color mycolor = new Color(R,G,B);
g.setColor(mycolor);
g.setBackground(Color.red);
//g.drawImage(theImg, 0, 0, null);
Font rotatefont=new Font(choosefont, Font.BOLD, 200);
rotatefont = Font(200);
g.setFont(rotatefont); // 字体、字型、字号 三个字350
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
FontMetrics rotatefm = g.getFontMetrics(font);
int rotatetextWidth = rotatefm.stringWidth(markContent);
int rotatewidthX = (width - rotatetextWidth) / 2;
//画倒转180度的字体
g.translate(rotatewidthX+rotatetextWidth, 350);
g.rotate(Math.toRadians(180));
g.drawString(markContent, 0, 0);
g.dispose();
try {
long currenttime = System.currentTimeMillis();
String pathdate = new SimpleDateFormat("yyyy/MM/dd/HH/hh/mm/ss").format(new Date());
String path = "F:\\pic2018\\"+pathdate;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
QRCodeUtil qr = new QRCodeUtil();
String qrCodepath = qr.Util(url, pathdate);
String outPath = path+"\\"+currenttime+".jpg";
FileOutputStream out = new FileOutputStream(outPath); // 先用一个特定的输出文件名
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(100, true); //
encoder.encode(bimage, param); InputStream is = new FileInputStream(outPath);
//通过JPEG图象流创建JPEG数据流解码器
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
//解码当前JPEG数据流,返回BufferedImage对象
BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
//得到画笔对象
Graphics g2 = buffImg.getGraphics(); //小图片的路径
ImageIcon QrcodeimgIcon = new ImageIcon(qrCodepath);
//得到Image对象。
Image img = QrcodeimgIcon.getImage();
//将小图片绘到大图片上,5,300 .表示你的小图片在大图片上的位置。
g2.drawImage(img, 50, 1560, null);
//设置颜色。
g2.setColor(Color.WHITE);
g2.dispose();
OutputStream os = new FileOutputStream(path+"\\"+currenttime+"zong"+".jpg");
//创键编码器,用于编码内存中的图象数据。
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(buffImg);
is.close();
os.close(); out.close();
} catch (Exception e) {
return false;
}
return true;
}
// 第一个参数是外部字体名,第二个是字体大小
public static Font loadFont(String fontFileName, float fontSize){
try{
File file = new File(fontFileName);
FileInputStream aixing = new FileInputStream(file);
Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
aixing.close();
return dynamicFontPt;
}
catch (Exception e)// 异常处理
{
e.printStackTrace();
return new Font("宋体", Font.BOLD, 200);
}
}
public Font Font(int fontsize) {
String root = System.getProperty("user.dir");// 项目根目录路径
Font font = ImageEdit.loadFont(root + "/FZXBSJW.TTF", fontsize);// 调用
return font;// 返回字体

java给图片写正反字体,并将二维码写到图片上,代码实现的更多相关文章

  1. js生成二维码以及插入图片

    先根据qrcode官网demo,不同属性值的变化,二维码的变化效果:https://larsjung.de/jquery-qrcode/latest/demo/ 进入demo中,审查元素查看里面引用的 ...

  2. pbfunc外部函数扩展应用-直接在Datawindow中生成QR二维码,非图片方式

    利用pbfunc外部函数在Datawindow中直接生成QR二维码,非图片方式.需要注意以下面几点: Datawindow的DataObject的单位必须为像素(Pixels). Datawindow ...

  3. jquery生成二维码并实现图片下载

    1.引入jquery的两个js文件 <script src="../scripts/erweima/jquery-1.10.2.min.js"></script& ...

  4. 图片的URL上传至阿里云OSS操作(微信小程序二维码返回的二进制上传到OSS)

    当我们从网络中获取一个URL的图片我们要存储到本地或者是私有的云时,我们可以这样操作  把url中的图片文件下载到本地(或者上传到私有云中)  public String uploadUrlToOss ...

  5. 【java】google的zxing架包生成二维码和读取二维码【可带文字和logo】

    承接RC4生成不重复字符串的需求之后,因为优惠码要方便用户使用的缘故,所以思来想去,觉得还是直接生成二维码给用户直接扫比较实用,也不用用户专门记录冗长的优惠码编号. ================= ...

  6. js将网址转为二维码并下载图片

    将一个网址转为二维码, 下面可以添加文字, 还提供下载功能 利用的是 GitHub上面的qrcode.js 和canvas <!DOCTYPE html> <html> < ...

  7. java二维码的生成与解析代码

    二维码,是一种采用黑白相间的平面几何图形通过相应的编码算法来记录文字.图片.网址等信息的条码图片.如下图 二维码的特点: 1.  高密度编码,信息容量大 可容纳多达1850个大写字母或2710个数字或 ...

  8. phpqrcode实现二维码(含图片)

    ---恢复内容开始--- 1,http://phpqrcode.sourceforge.net/ 下载 2,解压以后只需要一个文件 3,原生php测试: <?php include 'phpqr ...

  9. Springboot生成二维码并下载图片png支持打包成zip

    pom.xml <!--二维码--> <dependency> <groupId>com.google.zxing</groupId> <arti ...

  10. Asp.Net 合并图片(二维码和其他图片合并)

    需求:根据网址生成二维码,然后再把二维码生成到背景图片上,然后显示在网页中 pic.ashx <%@ WebHandler Language="C#" Class=" ...

随机推荐

  1. Excel映射到实体-easyexcel工具

    来源 项目需要把Excel进行解析,并映射到对象属性,实现类似Mybatis的ORM的效果.使用的方式是自定义注解+POI,这种方式代码复杂而且不易于维护. easyexcel是阿里巴巴开源的一个框架 ...

  2. sqlserver默认隔离级别下并发批量update同一张表引起的死锁

    提到死锁,最最常规的场景之一是Session1 以排它锁的方式锁定A表,请求B表,session2以排它锁的方式锁定B表,请求A表之类的,访问顺序不一致导致死锁的情况本文通过简化,测试这样一种稍显特殊 ...

  3. 常用的git和repo命令

    首先下图是git的流程图 相关概念 svn与git命令的对比 git常用命令 git log // 查看当前库的git log信息 git status ./ // 查看当前库的状态 git diff ...

  4. C++ std::array 基本用法

    #include <iostream> #include <string> #include <array> using namespace std; // htt ...

  5. centos安装jdk10

    下载一个jdk10文件到linux : wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=ac ...

  6. 剑指Offer-38.平衡二叉树(C++/Java)

    题目: 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 分析: 可以从根节点开始遍历每一个节点,求得节点左右子树的最大高度,判断是不是平衡二叉树.这样做的问题在于会重复遍历节点,造成不必要的浪费. 所 ...

  7. [译]Vulkan教程(27)Image

    [译]Vulkan教程(27)Image Images Introduction 入门 The geometry has been colored using per-vertex colors so ...

  8. 最强Linux shell工具Oh My Zsh 指南

    引言 笔者已经使用zsh一年多了,发现这个东东的功能太强大了.接下来,给大家推荐一下. 以下是oh-my-zsh部分功能 命令验证 在所有正在运行的shell中共享命令历史记录 拼写纠正 主题提示(A ...

  9. php中文乱码原因和维修方法

    一.首先是PHP网页的编码 1.如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/html; charset=gb2312”),静态页面添加,所有文 ...

  10. 【带着canvas去流浪(10)】文字烟花

    目录 一. 文字烟花 二. 动画原理 2.1 像素操作 2.2 烟花生成算法 2.3 计时器 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址 ...