資源簡介
類似于谷歌圖片搜索,用戶上傳一個圖片,谷歌顯示因特網(wǎng)中與此圖片相同或者相似的圖片。

代碼片段和文件信息
package?com.test.image;
import?java.awt.AlphaComposite;
import?java.awt.Color;
import?java.awt.Font;
import?java.awt.Graphics2D;
import?java.awt.Image;
import?java.awt.RenderingHints;
import?java.awt.geom.AffineTransform;
import?java.awt.image.BufferedImage;
import?java.awt.image.ColorModel;
import?java.awt.image.WritableRaster;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?javax.imageio.ImageIO;
import?com.sun.image.codec.jpeg.ImageFormatException;
import?com.sun.image.codec.jpeg.JPEGCodec;
import?com.sun.image.codec.jpeg.JPEGImageDecoder;
import?com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
?*?圖片工具類,主要針對圖片水印處理
?*?
?*?@author?WANGHONG
?*?
?*/
public?class?ImageHelper?{
//?項目根目錄路徑
public?static?final?String?path?=?System.getProperty(“user.dir“);
/**
?*?生成縮略圖?
?*?保存:ImageIO.write(BufferedImage?imgType[jpg/png/...]?File);
?*?
?*?@param?source
?*????????????原圖片
?*?@param?width
?*????????????縮略圖寬
?*?@param?height
?*????????????縮略圖高
?*?@param?b
?*????????????是否等比縮放
?*?*/
public?static?BufferedImage?thumb(BufferedImage?source?int?width?int?height?boolean?b)?{
//?targetW,targetH分別表示目標(biāo)長和寬
int?type?=?source.getType();
BufferedImage?target?=?null;
double?sx?=?(double)?width?/?source.getWidth();
double?sy?=?(double)?height?/?source.getHeight();
if?(b)?{
if?(sx?>?sy)?{
sx?=?sy;
width?=?(int)?(sx?*?source.getWidth());
}?else?{
sy?=?sx;
height?=?(int)?(sy?*?source.getHeight());
}
}
if?(type?==?BufferedImage.TYPE_CUSTOM)?{?//?handmade
ColorModel?cm?=?source.getColorModel();
WritableRaster?raster?=?cm.createCompatibleWritableRaster(width?height);
boolean?alphaPremultiplied?=?cm.isAlphaPremultiplied();
target?=?new?BufferedImage(cm?raster?alphaPremultiplied?null);
}?else
target?=?new?BufferedImage(width?height?type);
Graphics2D?g?=?target.createGraphics();
//?smoother?than?exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING?RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source?AffineTransform.getScaleInstance(sx?sy));
g.dispose();
return?target;
}
/**
?*?圖片水印
?*?
?*?@param?imgPath
?*????????????待處理圖片
?*?@param?markPath
?*????????????水印圖片
?*?@param?x
?*????????????水印位于圖片左上角的?x?坐標(biāo)值
?*?@param?y
?*????????????水印位于圖片左上角的?y?坐標(biāo)值
?*?@param?alpha
?*????????????水印透明度?0.1f?~?1.0f
?*?*/
public?static?void?waterMark(String?imgPath?String?markPath?int?x?int?y?float?alpha)?{
try?{
//?加載待處理圖片文件
Image?img?=?ImageIO.read(new?File(imgPath));
BufferedImage?image?=?new?BufferedImage(img.getWidth(null)?img.getHeight(null)?BufferedImage.TYPE_INT_RGB);
Graphics2D?g?=?image.createGraphics();
g.drawImage(img?0?0?null);
//?加載水印圖片文件
Image?src_biao?=?ImageIO.read(new?File(m
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????541??2013-03-27?13:20??ImageSearch\.classpath
?????文件????????387??2013-03-27?13:18??ImageSearch\.project
?????文件????????629??2011-11-28?19:43??ImageSearch\.settings\org.eclipse.jdt.core.prefs
?????文件?????????95??2013-03-27?13:18??ImageSearch\.settings\org.jboss.ide.eclipse.as.core.prefs
?????文件???????6193??2013-03-27?13:21??ImageSearch\bin\com\test\image\ImageHelper.class
?????文件???????4102??2013-03-27?13:21??ImageSearch\bin\com\test\image\ImageSearch.class
?????文件????????276??2013-03-27?13:19??ImageSearch\bin\readme.txt
?????文件??????47657??2011-09-15?14:10??ImageSearch\images\example1.jpg
?????文件???????8434??2011-11-29?13:51??ImageSearch\images\example2.jpg
?????文件??????28065??2012-04-17?13:21??ImageSearch\images\example3.jpg
?????文件??????24759??2011-11-29?13:53??ImageSearch\images\example4.jpg
?????文件??????40815??2011-11-29?13:51??ImageSearch\images\example5.jpg
?????文件??????11837??2012-04-17?13:31??ImageSearch\images\example6.jpg
?????文件??????63859??2011-11-29?13:59??ImageSearch\images\source.jpg
?????文件???????6876??2013-03-27?13:15??ImageSearch\src\com\test\image\ImageHelper.java
?????文件???????4968??2013-03-27?13:17??ImageSearch\src\com\test\image\ImageSearch.java
?????文件????????276??2013-03-27?13:19??ImageSearch\src\readme.txt
?????目錄??????????0??2013-03-27?13:21??ImageSearch\bin\com\test\image
?????目錄??????????0??2013-03-27?13:15??ImageSearch\src\com\test\image
?????目錄??????????0??2013-03-27?13:21??ImageSearch\bin\com\test
?????目錄??????????0??2013-03-27?13:15??ImageSearch\src\com\test
?????目錄??????????0??2013-03-27?13:21??ImageSearch\bin\com
?????目錄??????????0??2013-03-27?13:15??ImageSearch\src\com
?????目錄??????????0??2013-03-27?13:18??ImageSearch\.settings
?????目錄??????????0??2013-03-27?13:21??ImageSearch\bin
?????目錄??????????0??2011-11-29?14:05??ImageSearch\images
?????目錄??????????0??2011-11-29?09:03??ImageSearch\src
?????目錄??????????0??2011-11-29?09:03??ImageSearch
-----------?---------??----------?-----??----
???????????????249769????????????????????28
............此處省略1個文件信息
評論
共有 條評論