java图片加水印代码 最好有实例?。。∠刃涣耍 。?/h2>文字水印
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public class WaterSet {
/**
* 给图片添加水印
*
* @param filePath
*需要添加水印图片水印提取java代码的图片图片水印提取java代码的路径
* @param markContent
*水印图片水印提取java代码的文字
* @param markContentColor
*水印文字的颜色
* @param qualNum
*图片质量
* @return
*/
public boolean createMark(String filePath, String markContent,
Color markContentColor, float qualNum) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawString(markContent, width / 5, height / 5); // 添加水印的文字和设置水印文字出现的内容
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
}
图片水印
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 = https://www.04ip.com/post/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 = https://www.04ip.com/post/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="";
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("F:/logo.png","F:/123.jpg", 0, 0);
}
}
如何用java去除图片水?。?/h2>//运行以下程序即可
public class ImageInit {
BufferedImage image;
private int iw, ih;
private int[] pixels;
public ImageInit(BufferedImage image) {
this.image = image;
iw = image.getWidth();
ih = image.getHeight();
pixels = new int[iw * ih];
}
public BufferedImage changeGrey() {
PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih,
pixels, 0, iw);
try {
pg.grabPixels();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 设定二值化的域值 , 默认值为100
int grey = 100;
// 对图像进行二值化处理,Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault();
for (int i = 0; iiw * ih; i) {
int red, green, blue;
int alpha = cm.getAlpha(pixels[i]);
if (cm.getRed(pixels[i])grey) {
red = 255;
} else {
red = 0;
}
if (cm.getGreen(pixels[i])grey) {
green = 255;
} else {
green = 0;
}
if (cm.getBlue(pixels[i])grey) {
blue = 255;
} else {
blue = 0;
}
pixels[i] = alpha24 | red16 | green8 | blue; // 通过移位重新构成某一点像素的RGB值
}
// 将数组中的象素产生一个图像
Image tempImg = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(iw, ih, pixels, 0, iw));
image = new BufferedImage(tempImg.getWidth(null),
tempImg.getHeight(null), BufferedImage.TYPE_INT_BGR);
image.createGraphics().drawImage(tempImg, 0, 0, null);
return image;
}
public BufferedImage getMedian() {
PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih,
pixels, 0, iw);
try {
pg.grabPixels();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 对图像进行中值滤波 , Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault();
for (int i = 1; iih - 1; i) {
for (int j = 1; jiw - 1; j) {
int red, green, blue;
int alpha = cm.getAlpha(pixels[i * iwj]);
// int red2 = cm.getRed(pixels[(i - 1) * iwj]);
int red4 = cm.getRed(pixels[i * iwj - 1]);
int red5 = cm.getRed(pixels[i * iwj]);
int red6 = cm.getRed(pixels[i * iwj1]);
// int red8 = cm.getRed(pixels[(i1) * iwj]);
// 水平方向进行中值滤波
if (red4 = red5) {
if (red5 = red6) {
red = red5;
} else {
if (red4 = red6) {
red = red6;
} else {
red = red4;
}
}
} else {
if (red4red6) {
red = red4;
} else {
if (red5red6) {
red = red6;
} else {
red = red5;
}
}
}
int green4 = cm.getGreen(pixels[i * iwj - 1]);
int green5 = cm.getGreen(pixels[i * iwj]);
int green6 = cm.getGreen(pixels[i * iwj1]);
// 水平方向进行中值滤波
if (green4 = green5) {
if (green5 = green6) {
green = green5;
} else {
if (green4 = green6) {
green = green6;
} else {
green = green4;
}
}
} else {
if (green4green6) {
green = green4;
} else {
if (green5green6) {
green = green6;
} else {
green = green5;
}
}
}
// int blue2 = cm.getBlue(pixels[(i - 1) * iwj]);
int blue4 = cm.getBlue(pixels[i * iwj - 1]);
int blue5 = cm.getBlue(pixels[i * iwj]);
int blue6 = cm.getBlue(pixels[i * iwj1]);
// int blue8 = cm.getBlue(pixels[(i1) * iwj]);
// 水平方向进行中值滤波
if (blue4 = blue5) {
if (blue5 = blue6) {
blue = blue5;
} else {
if (blue4 = blue6) {
blue = blue6;
} else {
blue = blue4;
}
}
} else {
if (blue4blue6) {
blue = blue4;
} else {
if (blue5blue6) {
blue = blue6;
} else {
blue = blue5;
}
}
}
pixels[i * iwj] = alpha24 | red16 | green8
| blue;
}
}
// 将数组中的象素产生一个图像
Image tempImg = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(iw, ih, pixels, 0, iw));
image = new BufferedImage(tempImg.getWidth(null),
tempImg.getHeight(null), BufferedImage.TYPE_INT_BGR);
image.createGraphics().drawImage(tempImg, 0, 0, null);
return image;
}
public BufferedImage getGrey() {
ColorConvertOp ccp = new ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
return image = ccp.filter(image, null);
}
// Brighten using a linear formula that increases all color values
public BufferedImage getBrighten() {
RescaleOp rop = new RescaleOp(1.25f, 0, null);
return image = rop.filter(image, null);
}
// Blur by "convolving" the image with a matrix
public BufferedImage getBlur() {
float[] data = https://www.04ip.com/post/{ .1111f, .1111f, .1111f, .1111f, .1111f, .1111f,
.1111f, .1111f, .1111f, };
ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, data));
return image = cop.filter(image, null);
}
// Sharpen by using a different matrix
public BufferedImage getSharpen() {
float[] data = https://www.04ip.com/post/{ 0.0f, -0.75f, 0.0f, -0.75f, 4.0f, -0.75f, 0.0f,
-0.75f, 0.0f };
ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, data));
return image = cop.filter(image, null);
}
// 11) Rotate the image 180 degrees about its center point
public BufferedImage getRotate() {
AffineTransformOp atop = new AffineTransformOp(
AffineTransform.getRotateInstance(Math.PI,
image.getWidth() / 2, image.getHeight() / 2),
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
return image = atop.filter(image, null);
}
public BufferedImage getProcessedImg() {
return image;
}
public static void main(String[] args) throws IOException {
String filePath="F:/k7qp5.png";
FileInputStream fin = new FileInputStream(filePath);
BufferedImage bi = ImageIO.read(fin);
ImageInit flt = new ImageInit(bi);
flt.changeGrey();
flt.getGrey();
flt.getBrighten();
bi = flt.getProcessedImg();
String pname = filePath.substring(0, filePath.lastIndexOf("."));
File file = new File(pname".jpg");
ImageIO.write(bi, "jpg", file);
}
}
JAVA 数字水印 小程序(给图片打上水印或检测水?。蠓⒏鲈绰胄恍?/h2>package com.aspectj;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* @author Administrator
*
*TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*
*添加水印, filePath 源图片路径 含图片名,watermark 水印图片路径 savePath
*为你添加水印后的图片保存路径文件夹 words 要添加的文字
*/
// 添加水印,filePath 源图片路径 , watermark 水印图片路径
public class Mark {
private static int wid = 0;
private static int het = 0;
public static boolean createMark(String filePath, String watermark,
String words, String savePath) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
ImageIcon waterIcon = new ImageIcon(watermark);
Image waterImg = waterIcon.getImage();
// /////////////////////////////////////////////////////////////////////
File f = new File(filePath);
String picname = f.getName();// 取得图片名
if (watermark != null!watermark.equals("")) {// 当水印图标为空时
ImageIcon markIcon = new ImageIcon(watermark); // 要添加的水印图标
Image markImg = markIcon.getImage();
wid = markImg.getWidth(null); // 水印图标宽度
het = markImg.getHeight(null); // 水印图标高度
}
// ////////////////////////////////////////////////////////////////////
int width = theImg.getWidth(null); // 源图片宽度
int height = theImg.getHeight(null); // 源图片高度
if (savePath.equals(""))
savePath = filePath;// 如果未指定保存路径则保存回原路径
else
savePath = savePath"指定保存文件夹时,拼接出保存路径";
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(Color.red); // 设置颜色
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawImage(waterImg, width - wid5, height - het5, null); // 添加图标中间两个数字参数
// 是设定位置
g.drawString(words, width - 120, height - 10); // 添加文字
try {
FileOutputStream out = new FileOutputStream(savePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(50f, true); // 图片质量
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("===========水印失败");
return false;
} finally {
System.gc();// 清理 垃圾对象
}
System.out.println("===========水印成功");
return true;
}
// /测试主程序
public static void main(String[] args) {
createMark("dcc451da81cb39dbfa76de3ad2160924ab183023.jpg", "u=4038692558,3024950167fm=21gp=0.jpg", "aas", "");
}
}
检测水印很难做到 , 但是加上水印还是比较简单的
【图片水印提取java代码 图片去水印代码】关于图片水印提取java代码和图片去水印代码的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- erp管理系统的意义和价值,erp企业管理系统优点
- fluttercontainer阴影,flutter repaintboundary
- erp系统打印显示没有注册类,单据显示出错,没有注册类
- 网红刘老大直播完整版下载,大刘老哥
- VBnet实例化对象的简单介绍
- mysql5.7免安装方法,mysql57免安装教程
- 经典躲避飞行游戏,一款很难的飞行躲避游戏
- 最好的益智游戏,最好的益智游戏排行榜
- mysqlvs怎么使用 mysql v v