/**
* @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. input输入框change和blur事件区别

    blur与change事件在绝大部分的情况下表现都非常相似,输入结束后,离开输入框,会先后触发change与blur,唯有两点例外. 1.没有进行任何输入时,不会触发change 在这种情况下失焦后, ...

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

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

  3. Abusing SUDO Advance for Linux Privilege Escalation

    Index What is SUDO? Scenario. Sudoer FIle Syntax. Exploiting SUDO zip tar strace tcpdump nmap scp ex ...

  4. zipalign的使用

    zipalign 是一个存档对齐工具,可为Android应用程序(.apk)文件提供重要的优化.目的是确保所有未压缩数据以相对于文件开头的特定对齐开始.具体来说,它会导致.apk中的所有未压缩数据(如 ...

  5. [ERR] Node 172.16.6.154:7002 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

    关于启动redis集群时: [ERR] Node 172.168.63.202:7001 is not empty. Either the nodealready knows other nodes ...

  6. 小程序模板template使用介绍

    template(模板):是可以在wxml中引用的代码,就是在wxml中引用公用的wxml类型的代码,它的作用类似于组件,因此这里简单的说明下template与Component (组件)的区别. t ...

  7. SA详细注释不压行代码

    ){ //变量含义:m是字符集大小,n是字符串长度,c是一个桶数组,a[i]是字符串(下标从1开始) //rk[i]就是suffix(i)的字典序排名,sa[i]就是要求的排名为i的后缀的起始位置,即 ...

  8. NFS深度解析及搭建同步NFS服务

    1.nfs 进程 [root@nfsserver ~]# ps -ef|egrep "nfs|rpc" rpcuser : ? :: rpc.statd -->检查文件一致性 ...

  9. Python 爬虫从入门到进阶之路(四)

    之前的文章我们做了一个简单的例子爬取了百度首页的 html,我们用到的是 urlopen 来打开请求,它是一个特殊的opener(也就是模块帮我们构建好的).但是基本的 urlopen() 方法不支持 ...

  10. PAT 1010 Radix 进制转换+二分法

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...