Java圖片裁剪和生成縮略圖的實例辦法。本站提示廣大學習愛好者:(Java圖片裁剪和生成縮略圖的實例辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java圖片裁剪和生成縮略圖的實例辦法正文
一、縮略圖
在閱讀相冊的時分,能夠需求生成相應的縮略圖。
直接上代碼:
public class ImageUtil {
private Logger log = LoggerFactory.getLogger(getClass());
private static String DEFAULT_PREVFIX = "thumb_";
private static Boolean DEFAULT_FORCE = false;//建議該值為false
/**
* <p>Title: thumbnailImage</p>
* <p>Description: 依據圖片途徑生成縮略圖 </p>
* @param imagePath 原圖片途徑
* @param w 縮略圖寬
* @param h 縮略圖高
* @param prevfix 生成縮略圖的前綴
* @param force 能否強迫依照寬高生成縮略圖(假如為false,則生成最佳比例縮略圖)
*/
public void thumbnailImage(String imagePath, int w, int h, String prevfix, boolean force){
File imgFile = new File(imagePath);
if(imgFile.exists()){
try {
// ImageIO 支持的圖片類型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
String types = Arrays.toString(ImageIO.getReaderFormatNames());
String suffix = null;
// 獲取圖片後綴
if(imgFile.getName().indexOf(".") > -1) {
suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);
}// 類型和圖片後綴全部小寫,然後判別後綴能否合法
if(suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0){
log.error("Sorry, the image suffix is illegal. the standard image suffix is {}." + types);
return ;
}
log.debug("target image's size, width:{}, height:{}.",w,h);
Image img = ImageIO.read(imgFile);
if(!force){
// 依據原圖與要求的縮略圖比例,找到最適宜的縮略圖比例
int width = img.getWidth(null);
int height = img.getHeight(null);
if((width*1.0)/w < (height*1.0)/h){
if(width > w){
h = Integer.parseInt(new java.text.DecimalFormat("0").format(height * w/(width*1.0)));
log.debug("change image's height, width:{}, height:{}.",w,h);
}
} else {
if(height > h){
w = Integer.parseInt(new java.text.DecimalFormat("0").format(width * h/(height*1.0)));
log.debug("change image's width, width:{}, height:{}.",w,h);
}
}
}
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
g.dispose();
String p = imgFile.getPath();
// 將圖片保管在原目錄並加上前綴
ImageIO.write(bi, suffix, new File(p.substring(0,p.lastIndexOf(File.separator)) + File.separator + prevfix +imgFile.getName()));
log.debug("縮略圖在原途徑下生成成功");
} catch (IOException e) {
log.error("generate thumbnail image failed.",e);
}
}else{
log.warn("the image is not exist.");
}
}
public static void main(String[] args) {
new ImageUtil().thumbnailImage("C:/Users/cm/Desktop/我的頁面/images/girlNoImg.jpg" />
最後效果圖如上,左側可以拖動遷延,右側是預覽圖。
2、Java生成並保管剪切圖片
public class ImageUtil2 {
private Logger log = LoggerFactory.getLogger(getClass());
private static String DEFAULT_CUT_PREVFIX = "cut_";
/**
* Description: 依據原圖與裁切size截取部分圖片
* @param srcImg 源圖片
* @param output 圖片輸入流
* @param rect 需求截取局部的坐標和大小
*/
public void cutImage(File srcImg, OutputStream output,java.awt.Rectangle rect) {
if (srcImg.exists()) {
java.io.FileInputStream fis = null;
ImageInputStream iis = null;
try {
fis = new FileInputStream(srcImg);
// ImageIO 支持的圖片類型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG,
// JPEG, WBMP, GIF, gif]
String types = Arrays.toString(ImageIO.getReaderFormatNames())
.replace("]", ",");
String suffix = null;
// 獲取圖片後綴
if (srcImg.getName().indexOf(".") > -1) {
suffix = srcImg.getName().substring(srcImg.getName().lastIndexOf(".") + 1);
}// 類型和圖片後綴全部小寫,然後判別後綴能否合法
if (suffix == null
|| types.toLowerCase().indexOf(suffix.toLowerCase() + ",") < 0) {
log.error("Sorry, the image suffix is illegal. the standard image suffix is {}."+ types);
return;
}
// 將FileInputStream 轉換為ImageInputStream
iis = ImageIO.createImageInputStream(fis);
// 依據圖片類型獲取該品種型的ImageReader
ImageReader reader = ImageIO.getImageReadersBySuffix(suffix).next();
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, suffix, output);
log.info("圖片生成成功,請到目錄下檢查");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
if (iis != null)
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
log.warn("the src image is not exist.");
}
}
//生成目的文件途徑
public void cutImage(File srcImg, String destImgPath,java.awt.Rectangle rect) {
File destImg = new File(destImgPath);
if (destImg.exists()) {
String p = destImg.getPath();
try {
if (!destImg.isDirectory())
p = destImg.getParent();
if (!p.endsWith(File.separator))
p = p + File.separator;
cutImage(srcImg,new java.io.FileOutputStream(p + DEFAULT_CUT_PREVFIX+ "_"+ srcImg.getName()), rect);
} catch (FileNotFoundException e) {
log.warn("the dest image is not exist.");
}
} else
log.warn("the dest image folder is not exist.");
}
public void cutImage(String srcImg, String destImg, int x, int y, int width,
int height) {
cutImage(new File(srcImg), destImg, new java.awt.Rectangle(x, y, width, height));
}
public static void main(String[] args) {
new ImageUtil2().cutImage("C:/Users/cm/Desktop/我的頁面/images/boyNoImg.jpg" />
圖片中白色局部就是圖片剪切的div,我們可以經過拖拽剪裁區大小寬度等來察看改動了哪些參數,然後確定詳細對應的參數值辨別對應哪個參數值。如圖片中我的x/y/width/height辨別為40,28,224,228。
留意:在js中我將該div和圖片的長寬都定義為300*300,為配合測試所以我下載的圖片也為300*300。假如你測試的圖片大小不為300*300,那麼你直接在下面java中測試的效果會和你前端看到的不一樣,由於你前端的圖片寬高我定義為300*300,而你實踐圖片(即java中的圖片)不是。
那麼這個問題假如處置呢?
在你的java代碼中獲取原圖片的長寬,然後判別原圖片的長寬是不是300*300,假如不是就生成該圖片的300*300的縮略圖,然後將該300*300的縮略圖作為裁剪的圖片原型。(我的代碼中沒有處置,需求的自己處置,用完圖片之後刪除縮略圖)
2、Jcrop獲取參數

如圖,Jcrop直接提供了參數,可以直接運用。但是它有個缺陷就是在前端頁面的圖片大小區域不固定,假如你有個大像素圖片,那麼會十分丑,比方我在對應文件裡有個圖片soga_bak.jpg,換成這個圖片就不好了。
所以綜上建議用第一個js,然後判別原圖片的長寬是不是300,不是的生成300*300縮略圖,然後將縮略圖作為裁剪原型圖,用完了再刪除縮略圖。
以上所述是給大家引見的Java圖片裁剪和生成縮略圖的實例辦法,對Get懇求有效的問題,希望對大家有所協助,假如大家有任何疑問請給我留言,會及時回復大家的。在此也十分感激大家對網站的支持!