因项目需求把图片的DPI值提升到300,否则OCR识别产生错乱:直接上源码:
1、图片处理接口:
package util.image.dpi;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* 图片处理接口设计
* @author jffan
*
*/
public interface ImageDPIProcessor {
/**
* 根据文件后缀扩展名判断是否能进行处理
* @param fileName
* @return
*/
public boolean canHandle(String fileName);
/**
* 处理图片,设置图片DPI值
* @param path
* @param dpi dot per inch
* @return
* @throws IOException
*/
public byte[] process(final BufferedImage srcimage, final int dpi) throws Exception;
}
2、JEPG图片的实现类
package util.image.dpi;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageOutputStream;
import org.w3c.dom.Element;
/**
* JPEG 处理方式
* @author jffan
*
*/
public class JpegDPIProcessor implements ImageDPIProcessor {
private String formatName = "jp