惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
D
DataBreaches.Net
腾讯CDC
V
Visual Studio Blog
博客园 - 叶小钗
B
Blog
Apple Machine Learning Research
Apple Machine Learning Research
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
U
Unit 42
博客园 - 司徒正美
博客园 - 聂微东

博客园 - 刚泡

数据库短暂波动导致xxl-job-admin不调度任务的问题 CompletableFuture用法详解 使用责任链模式简化if-else代码示例 使用Function Interface简化if-else代码示例 自定义AngularJS Modal弹框大小的方法 SpringCloud Alibaba 改造Sentinel Dashboard将熔断规则持久化到Nacos SpringCloud Alibaba 改造Sentinel Dashboard将流控规则持久化到Nacos [Access][Microsoft][ODBC 驱动程序管理器] 无效的字符串或缓冲区长度 Invalid string or buffer length Java操作Kafka执行不成功的解决方法,Kafka Broker Advertised.Listeners属性的设置 pom.xml错误:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的解决方法 Spring Cloud @HystrixCommand和@CacheResult注解使用,参数配置 Spring Boot的属性加载顺序 Spring Boot 使用properties如何多环境配置 Spring Cloud微服务实战阅读笔记(一) 基础知识 Java使用POI为Excel打水印,调整列宽并设置Excel只读(用户不可编辑) Sitemesh 3使用及配置 Java上传文件夹(Jersey) 解决安卓微信浏览器中上传不能调用相机的问题 Mac下安装SVN插件javaHL not available的解决方法
路径名导致的异常:javax.imageio.IIOException: Can't r...
刚泡 · 2016-01-18 · via 博客园 - 刚泡

背景:

  写了一个测试程序,目的是读取本地的图片,为其打上水印图片。在使用过程中总会遇到: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

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