/**
* @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. sleuth和zipkin微服务里的链路跟踪

    分布式链路跟踪介绍 对于一个微服务系统,大多数来自外部的请求都会经过数个服务的互相调用,得到返回的结果,一旦结果回复较慢或者返回了不可用,我们就需要确定是哪个微服务出了问题.于是就有了分布式系统调用跟 ...

  2. Ligg.WinOa-000: Windows运维自动化编程实战--前言

        本开源项目Ligg.WinOa是一个基于Ligg.EasyWinApp的Windows运维自动化应用.通过Ligg.EasyWinForm生成2个功能界面:管理员工具箱和用户工具箱:通过Lig ...

  3. Redis中的Scan命令的使用

    Redis中有一个经典的问题,在巨大的数据量的情况下,做类似于查找符合某种规则的Key的信息,这里就有两种方式,一是keys命令,简单粗暴,由于Redis单线程这一特性,keys命令是以阻塞的方式执行 ...

  4. redis 开源客户端下载

    redis 开源客户端下载地址: https://github.com/qishibo/AnotherRedisDesktopManager/releases

  5. Paint.NET软件分享

    date: 2019-07-26 下载链接 官网链接 这是一款类Photoshop的轻量级图片编辑软件,仅有8.7MB.不多说话,直接上链接. 百度网盘链接 提取码:v4b2 软件简介 (百度百科警告 ...

  6. ios 免费抓包工具Stream

    ios 免费抓包工具Streamhttps://www.52pojie.cn/thread-1002406-1-1.html

  7. java之对象创建时各成员变量的初始值

    除了byte short int long float double char bollean这基础类型外,其余的都是引用类型 成员变量类型 初始值 byte 0 short 0 int 0 long ...

  8. Web安全测试学习笔记-DVWA-图片上传

    很多网站都有上传资源(图片或者文件)的功能,资源上传后一般会存储在服务器的一个文件夹里面,如果攻击者绕过了上传时候的文件类型验证,传了木马或者其他可执行的代码上去,那服务器就危险了. 我用DVWA的文 ...

  9. PlayJava Day017

    今日所学: /* 2019.08.19开始学习,此为补档. */ 1.数组变量 a.数组变量是数组的管理者而非数组本身 b.数组必须创建出来然后交给数组变量来管理 c.数组变量之间的赋值是管理权限的赋 ...

  10. Swoole中内置Http服务器

    创建httpServer.php文件,代码如下: <?php // 创建服务对象 $http = new swoole_http_server("10.211.55.17", ...