背景:

  写了一个测试程序,目的是读取本地的图片,为其打上水印图片。在使用过程中总会遇到:javax.imageio.IIOException: Can't read input file!的错误,最开始以为是图片路径名称写的不对,按照网上的提示换成正斜线和反斜线都不行。后来发现问题的原因是:图片的路径中不能有点(英文点:.);

  具体的错误异常提示如下:

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at com.bomei.image.ImageUtils.pressImage(ImageUtils.java:41)
at com.bomei.image.ImageUtils.main(ImageUtils.java:117)

  具体的代码如下:

package com.bomei.image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; public final class ImageUtils {
public ImageUtils() { } /*
* public final static String getPressImgPath() { return ApplicationContext
* .getRealPath("/template/data/util/shuiyin.gif"); }
*/ /**
* 把图片印刷到图片上
*
* @param pressImg --
* 水印文件
* @param targetImg --
* 目标文件
* @param x
* --x坐标
* @param y
* --y坐标
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y) {
try {
//目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null); //水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, (wideth - wideth_biao) / 2,
(height - height_biao) / 2, wideth_biao, height_biao, null);
//水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 打印文字水印图片
*
* @param pressText
* --文字
* @param targetImg --
* 目标图片
* @param fontName --
* 字体名
* @param fontStyle --
* 字体样式
* @param color --
* 字体颜色
* @param fontSize --
* 字体大小
* @param x --
* 偏移量
* @param y
*/ public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, int color, int fontSize, int x,
int y) {
try {
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// String s="www.qhd.com.cn";
g.setColor(Color.RED);
g.setFont(new Font(fontName, fontStyle, fontSize)); g.drawString(pressText, wideth - fontSize - x, height - fontSize
/ 2 - y);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
System.out.println(e);
}
} public static void main(String[] args) {
// pressImage("c:/test/safeqrcode.jpg","c:/test/poster.png", 0, 0);
pressImage("C:\\Users\\g.wang\\Desktop\\Two_Dimension_Code\\safeqrcode.jpg","c:/test/poster.png", 0, 0);
}
}

  如上面代码中main函数中所示,方法调用的第一个参数含有g.wang的路径,即含有英文的句号(.),这样就会导致这个上面的异常出现,将会在(g.drawImage)处报出异常。将图片的位置更改到一个不含有英文点的路径即可解决这个问题。如下:

pressImage("c:/test/safeqrcode.jpg","c:/test/poster.png", 0, 0);

  另外要说的一句是,网上好多的为图片打上水印图片的代码都不太好使,都是一个转一个的。找了好久才找到一篇好使的,原文链接如下:

  

http://my.oschina.net/jgy/blog/55702?fromerr=AuPHExZk

  这篇文章介绍的为图片打上水印图片和水印文字的方法是好使的。

路径名导致的异常:javax.imageio.IIOException: Can't read input file!的更多相关文章

  1. javax.imageio.IIOException: Can't read input file!完美解决

    今天遇到一个问题,上图 这段代码主要是给图片添加水印 后来百度发现可能是图片的路径出了问题,因为是动态获取的图片地址,然后我把地址打印出来了 之后通过终端查看,果然没有 之后我在classes目录找到 ...

  2. javax.imageio.IIOException: Can't create cache file!

    javax.imageio.IIOException: Can't create cache file! at javax.imageio.ImageIO.createImageInputStream ...

  3. 网页验证码出不来,读取验证码时出错:javax.imageio.IIOException: Can't create cache file!

    版权声明:本文为博主原创文章,仅作为学习交流使用 转载请注明出处 https://www.cnblogs.com/linck/p/10593053.html 今天打开项目时,发现登陆界面的验证码出不来 ...

  4. javax.imageio.IIOException: Can't create output stream! (生成验证码Servlet)

    在  web.xml里面加入 下面的代码: <servlet>         <servlet-name>validatecode</servlet-name> ...

  5. 解决tomcat报错javax.imageio.IIOException: Can't create output stream!

    启动tomcat catalina.out报错如下,登陆的时候无法显示验证码 2017-06-09 11:23:06,628 DEBUG org.springframework.web.servlet ...

  6. 解决javax.imageio.IIOException: Can't create output stream!

    解决javax.imageio.IIOException: Can't create output stream! javax.imageio.ImageIO.write(image, "J ...

  7. alias导致virtualenv异常的分析和解法

    title: alias导致virtualenv异常的分析和解法 toc: true comments: true date: 2016-06-27 23:40:56 tags: [OS X, ZSH ...

  8. VS2012外接程序VMDebugger未能加载或导致了异常

    转http://blog.csdn.net/maryhuan/article/details/42676915 故障现象:打开Visual Studio 2010后弹出错误框,外接程序VMDebugg ...

  9. Fragment已经被added了导致的异常。

    java.lang.IllegalStateException: Fragment already added:  ******Effect 出现的原因是commit方法提交是异步的,所以容易出现,判 ...

随机推荐

  1. 【每日scrum】第一次冲刺day2

    和小伙伴一起找地图 ,学习了mapinfo地图格式的基本知识,数据和图像分开存储

  2. 第二阶段Sprint冲刺会议3

     进展:讨论视频录制的具体功能,查看有关资料,开始着手编写有关代码.

  3. HDU 4123 Bob’s Race 树形dp+单调队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...

  4. pktgen-dpdk 运行 run.py 报错 Config file 'default' not found 解决方法

    pktgen 操作手册:http://pktgen-dpdk.readthedocs.io/en/latest/getting_started.html 执行到这一步时: $ cd <Pktge ...

  5. 前端基础(http协议相关篇)

    网络协议篇: 1.http请求过程 DNS解析——tcp三次握手——建立tcp连接后发起http请求——服务器响应http请求 ——浏览器得到资源——浏览器渲染 2.http报文 通用首部:可以出现在 ...

  6. TCP/IP Illustrated Vol1 Second Edition即英文版第二版,TCP部分个人勘误

    目前已经有了英文版第二版的TCPIP详解,中文版暂时还没有,但是英文版还是有好几处错误,作者和官方竟然没有维护一个勘误表. 个人阅读过程中针对TCP部分可能有问题的地方简单勘误一下 P596:示意图中 ...

  7. 理解jquery on 委托事件的机制

    前两天做了一个点击任意位置,都能关闭菜单的功能,因为菜单里面的每一个a,的点击事件都是用on绑定的.所以在阻止冒泡的时候不管用,今天特意来理解一下on的机制 on 是委托事件,利用的就是冒泡原理 $( ...

  8. [转帖] Linux buffer 和 cache相关内容

    Linux中Buffer/Cache清理 Lentil2018年9月6日 Linux中的buff/cache可以被手动释放,释放缓存的代码如下: https://lentil1016.cn/linux ...

  9. PHP中的PEAR是什么?

    PEAR也就是为PHP扩展与应用库(PHP Extension and Application Repository),它是一个PHP扩展及应用的一个代码仓库. 补充:php中扩展pecl与pear ...

  10. WEB javaScript

    javaScript 1.常规方法document.write("内容") :书写内容到网页中window.alert("内容") :网页警告弹窗 2.使用方法 ...