//運(yùn)行以下程序即可
10年專注成都網(wǎng)站制作,成都定制網(wǎng)頁(yè)設(shè)計(jì),個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)頁(yè)設(shè)計(jì),高端網(wǎng)頁(yè)制作,對(duì)鑿毛機(jī)等多個(gè)方面,擁有多年的營(yíng)銷推廣經(jīng)驗(yàn)。
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();
}
//?設(shè)定二值化的域值,默認(rèn)值為100
int?grey?=?100;
//?對(duì)圖像進(jìn)行二值化處理,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;?//?通過(guò)移位重新構(gòu)成某一點(diǎn)像素的RGB值
}
//?將數(shù)組中的象素產(chǎn)生一個(gè)圖像
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();
}
//?對(duì)圖像進(jìn)行中值濾波,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]);
//?水平方向進(jìn)行中值濾波
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]);
//?水平方向進(jìn)行中值濾波
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]);
//?水平方向進(jìn)行中值濾波
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;
}
}
//?將數(shù)組中的象素產(chǎn)生一個(gè)圖像
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);
}
}
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?要更改此生成的類型注釋的模板,請(qǐng)轉(zhuǎn)至?窗口?-?首選項(xiàng)?-?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(""))?{//?當(dāng)水印圖標(biāo)為空時(shí)
ImageIcon?markIcon?=?new?ImageIcon(watermark);?//?要添加的水印圖標(biāo)
Image?markImg?=?markIcon.getImage();
wid?=?markImg.getWidth(null);?//?水印圖標(biāo)寬度
het?=?markImg.getHeight(null);?//?水印圖標(biāo)高度
}
//?////////////////////////////////////////////////////////////////////
int?width?=?theImg.getWidth(null);?//?源圖片寬度
int?height?=?theImg.getHeight(null);?//?源圖片高度
if?(savePath.equals(""))
savePath?=?filePath;//?如果未指定保存路徑則保存回原路徑
else
savePath?=?savePath?+?"指定保存文件夾時(shí),拼接出保存路徑";
BufferedImage?bimage?=?new?BufferedImage(width,?height,
BufferedImage.TYPE_INT_RGB);
Graphics2D?g?=?bimage.createGraphics();
g.setColor(Color.red);?//?設(shè)置顏色
g.setBackground(Color.white);
g.drawImage(theImg,?0,?0,?null);
g.drawImage(waterImg,?width?-?wid?+?5,?height?-?het?+?5,?null);?//?添加圖標(biāo)中間兩個(gè)數(shù)字參數(shù)
//?是設(shè)定位置
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);?//?圖片質(zhì)量
encoder.encode(bimage,?param);
out.close();
}?catch?(Exception?e)?{
e.printStackTrace();
System.out.println("===========水印失敗");
return?false;
}?finally?{
System.gc();//?清理?垃圾對(duì)象
}
System.out.println("===========水印成功");
return?true;
}
//?/測(cè)試主程序
public?static?void?main(String[]?args)?{
createMark("dcc451da81cb39dbfa76de3ad2160924ab183023.jpg",?"u=4038692558,3024950167fm=21gp=0.jpg",?"aas",?"");
}
}
檢測(cè)水印很難做到,但是加上水印還是比較簡(jiǎn)單的
帶有水印的圖片,是合成過(guò)的
所以,用ps修補(bǔ)簡(jiǎn)單易用,如果用java去實(shí)現(xiàn)類似ps的功能,難度可想而知,建議換種思路考慮這個(gè)問(wèn)題
文字水印
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
* 圖片質(zhì)量
* @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); // 添加水印的文字和設(shè)置水印文字出現(xiàn)的內(nèi)容
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 --
* 目標(biāo)文件
* @param x
* --x坐標(biāo)
* @param y
* --y坐標(biāo)
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y) {
try {
//目標(biāo)文件
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);
//水印文件結(jié)束
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 --
* 目標(biāo)圖片
* @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);
}
}
jpg文件上的水印的清除方法:
如果需要將帶水印的JPG轉(zhuǎn)換成05H的PDG:
1、 將PDG批量更名為JPG。如果下載的時(shí)候就已經(jīng)是JPG,則此步省略。
2、用ComicEnhancer Pro打開(kāi)帶水印的JPG,色彩選“單色”,水印沒(méi)了吧?不過(guò)這個(gè)時(shí)候文字多半也會(huì)變得很細(xì),可以通過(guò)增加“Gamma校正”值,或用“曲線”來(lái)加黑。注意“Gamma校正”和“曲線”選一個(gè)足矣。調(diào)節(jié)好以后,轉(zhuǎn)換成TIFF。
3、將TIFF文件更名為PDG,并且符合PDG文件命名規(guī)范,然后用高版本DjVuToy的“PDG壓縮”功能轉(zhuǎn)換成05H的PDG。注意轉(zhuǎn)換的時(shí)候把“轉(zhuǎn)換為快速版”選項(xiàng)去掉。
如果不需要轉(zhuǎn)換成PDG,而是希望在去掉水印的同時(shí)盡可能保持清晰:
1、將PDG批量更名為JPG。如果下載的時(shí)候就已經(jīng)是JPG,則此步省略。
2、用ComicEnhancer Pro打開(kāi)帶水印的JPG,將“高亮度”設(shè)置為125,看到那神奇的效果了嗎?如果希望對(duì)文字的影響盡可能小,還可以嘗試將“高亮值”設(shè)置為210。
3、下面就看你高興了,可以直接存為JPG,也可以在色彩選“16級(jí)灰度”、“8級(jí)灰度”、“4級(jí)灰度”,然后轉(zhuǎn)換成PNG?;叶燃?jí)數(shù)越少,圖像損失越多,文件越小,16級(jí)灰度基本上肉眼看不出文字部分有任何損失,4級(jí)灰度則很明顯,可以結(jié)合“曲線”或“Gamma校正”等加以改善。
帶有水印的圖片,是合成過(guò)的 所以,用ps修補(bǔ)簡(jiǎn)單易用,如果用java去實(shí)現(xiàn)類似ps的功能,難度可想而知,建議換種思路考慮這個(gè)問(wèn)題
本文題目:java代碼去除圖片水印 java去除圖片水印算法
本文路徑:http://aaarwkj.com/article12/docpgdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、移動(dòng)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、App設(shè)計(jì)、域名注冊(cè)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)