java代码去除图片水印 java去除图片水印算法

如何用java去除图片水印?

//运行以下程序即可

10年专注成都网站制作,成都定制网页设计,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于成都定制网页设计,高端网页制作,对凿毛机等多个方面,拥有多年的营销推广经验。

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; i  iw * 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] = alpha  24 | red  16 | green  8 | 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; i  ih - 1; i++) {

for (int j = 1; j  iw - 1; j++) {

int red, green, blue;

int alpha = cm.getAlpha(pixels[i * iw + j]);

// int red2 = cm.getRed(pixels[(i - 1) * iw + j]);

int red4 = cm.getRed(pixels[i * iw + j - 1]);

int red5 = cm.getRed(pixels[i * iw + j]);

int red6 = cm.getRed(pixels[i * iw + j + 1]);

// int red8 = cm.getRed(pixels[(i + 1) * iw + j]);

// 水平方向进行中值滤波

if (red4 = red5) {

if (red5 = red6) {

red = red5;

} else {

if (red4 = red6) {

red = red6;

} else {

red = red4;

}

}

} else {

if (red4  red6) {

red = red4;

} else {

if (red5  red6) {

red = red6;

} else {

red = red5;

}

}

}

int green4 = cm.getGreen(pixels[i * iw + j - 1]);

int green5 = cm.getGreen(pixels[i * iw + j]);

int green6 = cm.getGreen(pixels[i * iw + j + 1]);

// 水平方向进行中值滤波

if (green4 = green5) {

if (green5 = green6) {

green = green5;

} else {

if (green4 = green6) {

green = green6;

} else {

green = green4;

}

}

} else {

if (green4  green6) {

green = green4;

} else {

if (green5  green6) {

green = green6;

} else {

green = green5;

}

}

}

// int blue2 = cm.getBlue(pixels[(i - 1) * iw + j]);

int blue4 = cm.getBlue(pixels[i * iw + j - 1]);

int blue5 = cm.getBlue(pixels[i * iw + j]);

int blue6 = cm.getBlue(pixels[i * iw + j + 1]);

// int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);

// 水平方向进行中值滤波

if (blue4 = blue5) {

if (blue5 = blue6) {

blue = blue5;

} else {

if (blue4 = blue6) {

blue = blue6;

} else {

blue = blue4;

}

}

} else {

if (blue4  blue6) {

blue = blue4;

} else {

if (blue5  blue6) {

blue = blue6;

} else {

blue = blue5;

}

}

}

pixels[i * iw + j] = alpha  24 | red  16 | green  8

| 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 = { .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 = { 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 数字水印 小程序(给图片打上水印或检测水印),求发个源码谢谢

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 - wid + 5, height - het + 5, 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 如何去掉图片水印

带有水印的图片,是合成过的

所以,用ps修补简单易用,如果用java去实现类似ps的功能,难度可想而知,建议换种思路考虑这个问题

java图片加水印代码 最好有实例!!!先谢了!!

文字水印

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.swing.*;

import com.sun.image.codec.jpeg.*;

public class WaterSet {

/**

* 给图片添加水印

*

* @param filePath

* 需要添加水印的图片的路径

* @param markContent

* 水印的文字

* @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 = 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="";

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给jpg图片加文字水印,加的水印盖住了原来的图片,怎么办。

jpg文件上的水印的清除方法:

如果需要将带水印的JPG转换成05H的PDG:

1、 将PDG批量更名为JPG。如果下载的时候就已经是JPG,则此步省略。

2、用ComicEnhancer Pro打开带水印的JPG,色彩选“单色”,水印没了吧?不过这个时候文字多半也会变得很细,可以通过增加“Gamma校正”值,或用“曲线”来加黑。注意“Gamma校正”和“曲线”选一个足矣。调节好以后,转换成TIFF。

3、将TIFF文件更名为PDG,并且符合PDG文件命名规范,然后用高版本DjVuToy的“PDG压缩”功能转换成05H的PDG。注意转换的时候把“转换为快速版”选项去掉。

如果不需要转换成PDG,而是希望在去掉水印的同时尽可能保持清晰:

1、将PDG批量更名为JPG。如果下载的时候就已经是JPG,则此步省略。

2、用ComicEnhancer Pro打开带水印的JPG,将“高亮度”设置为125,看到那神奇的效果了吗?如果希望对文字的影响尽可能小,还可以尝试将“高亮值”设置为210。

3、下面就看你高兴了,可以直接存为JPG,也可以在色彩选“16级灰度”、“8级灰度”、“4级灰度”,然后转换成PNG。灰度级数越少,图像损失越多,文件越小,16级灰度基本上肉眼看不出文字部分有任何损失,4级灰度则很明显,可以结合“曲线”或“Gamma校正”等加以改善。

如何用java去除图片水印

带有水印的图片,是合成过的 所以,用ps修补简单易用,如果用java去实现类似ps的功能,难度可想而知,建议换种思路考虑这个问题

本文题目:java代码去除图片水印 java去除图片水印算法
本文路径:https://www.cdcxhl.com/article12/docpgdc.html

成都网站建设公司_创新互联,为您提供移动网站建设自适应网站App设计域名注册网站改版

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管