当前位置:网站首页>ImgUtil 图片处理工具类,文字提取,图片水印
ImgUtil 图片处理工具类,文字提取,图片水印
2022-06-29 15:04:00 【何xiao树】
咸来无事,整理了一些可能会用到的 图片处理工具,↓
<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j 图像识别 -->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.12</version>
</dependency>
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.util.ResourceUtils;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/** * @description: 图片处理工具类 * @author: hexiaoshu **/
@Slf4j
public class ImgUtil {
/** * 图片内容提取,数字以前单次读取较准确,中文需要训练 * @param imgPath 图片路径 * @param languagePath 语言库路径 * @param language 语言,默认英文 chi_sim 中文, eng 英文 * @param rectangle 坐标范围,可选 * @return Content */
public static String readImg(String imgPath,String languagePath,String language,Rectangle rectangle){
File file = new File(imgPath);
if (!file.exists()){
return "图片路径不存在";
}
ITesseract instance = new Tesseract();
instance.setDatapath(languagePath);
if (language==null || language.isEmpty()){
instance.setLanguage("eng");
}
String result = "";
try {
if (rectangle==null){
result = instance.doOCR(file);
}else {
result = instance.doOCR(file ,rectangle);
}
} catch (TesseractException e) {
e.printStackTrace();
}
return result;
}
/** 设置文字水印 * @param sourceImg 源图片路径 * @param targetImg 保存的图片路径 * @param watermark 水印内容 * @param color 水印颜色 new Color(255,255,255,128) * @param font 水印字体 new Font("04b_08", Font.PLAIN, 30) * @throws IOException */
public static boolean addWatermark(String sourceImg, String targetImg, String watermark,Color color,Font font){
File srcImgFile = new File(sourceImg);
Image srcImg;
try {
srcImg=ImageIO.read(srcImgFile);
}catch (IOException e){
return false;
}
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
g.setColor(color);
g.setFont(font);
//设置水印的坐标
int x = srcImgWidth - (g.getFontMetrics(g.getFont()).charsWidth(watermark.toCharArray(), 0, watermark.length())+20);
int y = srcImgHeight - 25;
//加水印
g.drawString(watermark, x, y);
g.dispose();
// 输出图片
FileOutputStream outImgStream;
try {
outImgStream = new FileOutputStream(targetImg);
ImageIO.write(bufImg, "jpg", outImgStream);
outImgStream.flush();
outImgStream.close();
}catch (IOException e){
return false;
}
return true;
}
/** * 获取跟目录---与jar包同级目录的upload目录下指定的子目录subdirectory * @param subdirectory 子目录 * @return */
public static String getJarPath(String subdirectory){
File upload;
try {
//本地测试时获取到的是"工程目录/target/upload/subdirectory
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) {
path = new File("");
}
upload = new File(path.getAbsolutePath(),subdirectory);
if(!upload.exists()) {
upload.mkdirs();
}
return upload + File.separator;
} catch (FileNotFoundException e) {
throw new RuntimeException("获取服务器路径发生错误!");
}
}
/** * 为图片添加图片水印 * @param watermarkUrl 水印图片 * @param source 原图 * @param output 制作完成的图片 * @return boolean */
public static boolean markImgMark(String watermarkUrl, String source, String output){
File file = new File(source);
Image img;
try {
img = ImageIO.read(file);
}catch (IOException e){
e.printStackTrace();
return false;
}
String thumbnailName = "";
String thumbnailPath = "";
String path = getJarPath("thumbnail_img");
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
ImageIcon imgIcon = new ImageIcon(watermarkUrl);
int waterHeight = imgIcon.getImage().getHeight(null);
int waterWidth = imgIcon.getImage().getWidth(null);
if ( (waterHeight+waterWidth) > 400){
log.info("调整水印图片");
thumbnailName ="Thumbnail.png";
thumbnailPath=path+File.separator+thumbnailName;
ImgUtil.creatThumbnail(watermarkUrl, thumbnailPath,100,100);
imgIcon = new ImageIcon(thumbnailPath);
}
Image con = imgIcon.getImage();
float clarity = 0.6f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, clarity));
g.drawImage(con, 10, 10, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
File sf = new File(output);
try {
ImageIO.write(bi, "jpg", sf);
}catch (IOException e){
e.printStackTrace();
return false;
}
if (!thumbnailName.isEmpty()){
File file1 = new File(thumbnailPath);
file1.delete();
}
return true;
}
/** * 生成缩略图 * @param sourceImg 原图片 * @param outImg 生成图片 * @param width and height 图片宽高 * @return boolean */
public static boolean creatThumbnail(String sourceImg,String outImg,int width,int height){
File originalImg = new File(sourceImg);
File thumbnailImg = new File(outImg);
Thumbnails.Builder<File> builder = Thumbnails.of(originalImg).size(width, height).outputQuality(0.5f);
try {
builder.toFile(thumbnailImg);
}catch (IOException e){
e.printStackTrace();
return false;
}
return true;
}
}
tess4j 语言库 中文-英文.rar ,中文需要自个调整。
我觉得读取什么,身份证号码,驾驶证什么的,完全没有问题,而且还可以根据图片像素坐标提取, 问题不大。
链接:https://pan.baidu.com/s/1uSDw_dBpy9jmSpAcqkBYsA
提取码:fkq8
下次再添加。
边栏推荐
- js获取上个月第一天以及本月的最后一天
- 广州期货正规吗?如果有人喊你用自己的手机登,帮忙开户,安全吗?
- 中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]
- 使用自定义注解实现Redis分布式锁
- 阿里云体验有奖:使用PolarDB-X与Flink搭建实时数据大屏
- MCS:多元随机变量——多项式分布
- Analysis of constant current source circuit composed of two NPN tubes
- kubernetes Unable to connect to the server: x509: certificate has expired or is not yet valid
- 信息学奥赛一本通1002:输出第二个整数
- Real software testers = "half product + Half development"?
猜你喜欢

MCS: multivariate random variable - discrete random variable

Informatics Olympiad all in one 1194: mobile route

Render follows, encapsulating a form and adding data to the table

Unity C basic review 27 - delegation example (p448)

MCS: discrete random variable - Hyper geometric distribution

揭秘百度智能测试在测试自动执行领域实践

知识点:PCB线路板布线都有哪些诀窍?

明德扬XILINX-K7-325T/410T核心板数据手册

Uncover the practice of Baidu intelligent test in the field of automatic test execution

雷达基本组成
随机推荐
Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem
服务器的数据库连不上了【服务已起、防火墙已关、端口已开、netlent 端口不通】
Lumiprobe 活性染料丨氨基染料:花青5胺
MCS:多元随机变量——多项式分布
EMC surge protection and decoupling design
数字图像处理复习
上次给我们发福利的 TDesign ,今天讲讲它的开源故事
阿尔兹海默病智能诊断
第九章 APP项目测试(此章完结)
Informatics Olympiad all in one 1000: introductory test questions
SOFARegistry 源码|数据同步模块解析
SSL V**技术原理
MySQL的json 数组操作 json_array_append、json_array_insert
Is it safe to open a stock account at present? Can I open an account online directly
Hi, you have a code review strategy to check
Lumiprobe 脱氧核糖核酸丨磷酸盐 CPG 1000 固体载体
Lumiprobe deoxyribonucleic acid phosphate CpG 1000 solid carrier
MCS: discrete random variable Poisson distribution
Solution to the problem that the assembly drawing cannot be recognized after the storage position of SolidWorks part drawing is changed
请说下redis命令的时间复杂度??(实际问的是redis底层结构)
