一般上傳圖片分三步:
第一步:將原始圖片上傳到服務(wù)器端保存,不妨命名為src.jpg
第二步:在瀏覽器端將src.jpg顯示出來(lái),然后使用jQuery,對(duì)其進(jìn)行“客戶端剪切”。
所謂客戶端剪切就是根據(jù)用戶在界面中對(duì)原始圖片放大,移動(dòng),剪切時(shí),獲得一些參數(shù)。
具體需要六個(gè)參數(shù)(srcWidth, srcHeight, rect.x, rect.y , rect.width, rect.height)參數(shù)。
其中,srcWidth 和srcHeight表明原始圖片在客戶端放大后的寬和高
rect.x和rect.y表明剪切部分相對(duì)(srcWidth, srcHeight)的起始坐標(biāo)
rect.width和rect.height表示需要剪切的圖片的大小。
第三步:獲取第二步得到的參數(shù),對(duì)內(nèi)存中原始圖片先進(jìn)行預(yù)處理(按照srcWidth和srcHeight進(jìn)行縮放)、剪切。
然后對(duì)預(yù)處理后的內(nèi)存圖像剪切,打印出來(lái)。
第一步實(shí)現(xiàn)比較簡(jiǎn)單,第二步需要學(xué)習(xí)jQuery,網(wǎng)上例子很多。我們重點(diǎn)討論第三步, java切圖。
1.main函數(shù)
Java代碼 
- package syj.main;
-
- import java.awt.Rectangle;
- import java.io.File;
- import java.io.IOException;
-
- import syj.util.ImageHepler;
-
- public class Test {
- public static void main(String[] args) throws IOException {
- String picPath = "img/src.jpg";
- String destPath = "img/result.jpg";
- Rectangle rect = new Rectangle(0, 0, 2000, 1200);
- ImageHepler.cut(picPath, destPath, 1440, 900, rect);
- }
- }
2.關(guān)鍵圖像操作函數(shù)
Java代碼 
- package syj.util;
-
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintStream;
- import javax.imageio.ImageIO;
-
- public class ImageHepler {
-
- /**
- * @Description: 將srcImageFile裁剪后生成destImageFile
- * @param srcImageFile 原始圖
- * @param destImageFile 目標(biāo)圖
- * @param width 原始圖預(yù)處理后width
- * @param height 原始圖預(yù)處理后height
- * @param rect 目標(biāo)圖輸出的格式(rect.x, rect.y, rect.width, rect.height)
- * @throws IOException
- * @author Sun Yanjun
- */
- public static void cut(String srcImageFile, String destImageFile, int width, int height, Rectangle rect) throws IOException {
- Image image = ImageIO.read(new File(srcImageFile));
- BufferedImage bImage = makeThumbnail(image, width, height);
-
- //把原始圖片輸出
- ImageIO.write(bImage, "jpg",new File("img/src2.jpg"));
-
- saveSubImage(bImage, rect, new File(destImageFile));
- }
-
-
- /**
- * @Description: 將srcImageFile裁剪后生成destImageFile
- * @param srcImageFile 原始圖
- * @param destImageFile 目標(biāo)圖
- * @param width 原始圖預(yù)處理后width
- * @param height 原始圖預(yù)處理后height
- * @param rect 目標(biāo)圖輸出的格式(rect.x, rect.y, rect.width, rect.height)
- * @throws IOException
- * @author Sun Yanjun
- */
- public static void cut(File srcImageFile, File destImageFile, int width, int height, Rectangle rect) throws IOException {
- Image image = ImageIO.read(srcImageFile);
- BufferedImage bImage = makeThumbnail(image, width, height);
-
-
- saveSubImage(bImage, rect, destImageFile);
- }
-
- /**
- * @Description: 對(duì)原始圖片根據(jù)(x, y, width, height) = (0, 0, width, height)進(jìn)行縮放,生成BufferImage
- * @param img
- * @param width 預(yù)處理后圖片的寬度
- * @param height 預(yù)處理后圖片高度
- * @return
- * @author Sun Yanjun
- * @throws IOException
- */
- private static BufferedImage makeThumbnail(Image img, int width, int height) throws IOException {
- BufferedImage tag = new BufferedImage(width, height, 1);
- Graphics g = tag.getGraphics();
- g.drawImage(img.getScaledInstance(width, height, 4), 0, 0, null);
-
- g.dispose();
- return tag;
- }
-
- /**
- * @Description: 對(duì)BufferImage按照(x, y, width, height) = (subImageBounds.x, subImageBounds.y, subImageBounds.width, subImageBounds.height)進(jìn)行裁剪
- * 如果subImageBounds范圍過(guò)大,則用空白填充周圍的區(qū)域。
- *
- * @param image
- * @param subImageBounds
- * @param destImageFile
- * @throws IOException
- * @author Sun Yanjun
- */
- private static void saveSubImage(BufferedImage image, Rectangle subImageBounds, File destImageFile) throws IOException {
- String fileName = destImageFile.getName();
- String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
- BufferedImage subImage = new BufferedImage(subImageBounds.width, subImageBounds.height, 1);
- Graphics g = subImage.getGraphics();
-
-
- if ((subImageBounds.width > image.getWidth())
- || (subImageBounds.height > image.getHeight())) {
- int left = subImageBounds.x;
- int top = subImageBounds.y;
- if (image.getWidth() < subImageBounds.width)
- left = (subImageBounds.width - image.getWidth()) / 2;
- if (image.getHeight() < subImageBounds.height)
- top = (subImageBounds.height - image.getHeight()) / 2;
- g.setColor(Color.white);
- g.fillRect(0, 0, subImageBounds.width, subImageBounds.height);
- g.drawImage(image, left, top, null);
- ImageIO.write(image, formatName, destImageFile);
- } else {
- g.drawImage(image.getSubimage(subImageBounds.x, subImageBounds.y,
- subImageBounds.width, subImageBounds.height), 0, 0, null);
- }
- g.dispose();
- ImageIO.write(subImage, formatName, destImageFile);
- }
- }
-
-
BufferedImage subImage = new BufferedImage(subImageBounds.width, subImageBounds.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = subImage.createGraphics();
// 解決圖片背景透明問(wèn)題--且只針對(duì)png
subImage = g.getDeviceConfiguration().createCompatibleImage(subImageBounds.width, subImageBounds.height, Transparency.TRANSLUCENT);
g.dispose();
g = subImage.createGraphics();
|