一、前言
通过java.awt.Graphics、java.awt.Image图形图像处理包定义CropImageUtil图片剪切工具类,对原图进行任意位置区域剪切处理,如下1.jpg图,下面通过代码示例部分进行处理。
二、代码示例
import java.awt.Graphics;@b@import java.awt.GraphicsConfiguration;@b@import java.awt.GraphicsDevice;@b@import java.awt.GraphicsEnvironment;@b@import java.awt.HeadlessException;@b@import java.awt.Image;@b@import java.awt.Transparency;@b@import java.awt.image.BufferedImage;@b@import java.awt.image.ColorModel;@b@import java.awt.image.PixelGrabber;@b@import java.io.File;@b@import java.io.FileOutputStream;@b@import java.io.OutputStream;@b@@b@import javax.imageio.ImageIO;@b@import javax.swing.ImageIcon;@b@@b@/**@b@ * 图片剪切工具类@b@ */@b@public class CropImageUtil{@b@ @b@ static String sourceDirPath;@b@ static String targetDirPath;@b@@b@ /**@b@ * 图片剪切@b@ * @param sourceFileName原始文件名@b@ * @param targetFileName目标文件名@b@ * @param x X坐标@b@ * @param y Y坐@b@ * @param w 宽度@b@ * @param h 高度@b@ */@b@ public static void cropImage(String sourceFileName, String targetFileName,@b@ int x, int y, int w, int h) {@b@ String sourcePath = sourceDirPath + "\\" + sourceFileName;@b@@b@ try {@b@ File tempFile = new File(targetDirPath + "\\" + targetFileName);@b@ OutputStream tmp = new FileOutputStream(tempFile);@b@@b@ Image image = new ImageIcon(sourcePath).getImage();@b@ BufferedImage outImage = toBufferedImage(image);@b@ BufferedImage img = outImage.getSubimage(x, y, w, h);@b@ ImageIO.write(img, "jpg", tempFile);@b@ tmp.close();@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ public static BufferedImage toBufferedImage(Image image) {@b@ if (image instanceof BufferedImage) {@b@ return (BufferedImage) image;@b@ }@b@ // This code ensures that all the pixels in the image are loaded@b@ image = new ImageIcon(image).getImage();@b@ // Determine if the image has transparent pixels; for this method's@b@ // implementation, see e661 Determining If an Image Has Transparent@b@ // Pixels@b@ boolean hasAlpha = hasAlpha(image);@b@ // boolean hasAlpha = false;@b@ // Create a buffered image with a format that's compatible with the@b@ // screen@b@ BufferedImage bimage = null;@b@ GraphicsEnvironment ge = GraphicsEnvironment@b@ .getLocalGraphicsEnvironment();@b@ try {@b@ // Determine the type of transparency of the new buffered image@b@ int transparency = Transparency.OPAQUE;@b@ if (hasAlpha) {@b@ transparency = Transparency.BITMASK;@b@ }@b@ // Create the buffered image@b@ GraphicsDevice gs = ge.getDefaultScreenDevice();@b@ GraphicsConfiguration gc = gs.getDefaultConfiguration();@b@ bimage = gc.createCompatibleImage(image.getWidth(null), image@b@ .getHeight(null), transparency);@b@ } catch (HeadlessException e) {@b@ // The system does not have a screen@b@ }@b@ if (bimage == null) {@b@ // Create a buffered image using the default color model@b@ int type = BufferedImage.TYPE_INT_RGB;@b@ if (hasAlpha) {@b@ type = BufferedImage.TYPE_INT_ARGB;@b@ }@b@ bimage = new BufferedImage(image.getWidth(null), image@b@ .getHeight(null), type);@b@ }@b@ // Copy image to buffered image@b@ Graphics g = bimage.createGraphics();@b@ // Paint the image onto the buffered image@b@ g.drawImage(image, 0, 0, null);@b@ g.dispose();@b@ return bimage;@b@ }@b@@b@ // This method returns true if the specified image has transparent pixels@b@ public static boolean hasAlpha(Image image) {@b@ // If buffered image, the color model is readily available@b@ if (image instanceof BufferedImage) {@b@ BufferedImage bimage = (BufferedImage) image;@b@ return bimage.getColorModel().hasAlpha();@b@ }@b@ // Use a pixel grabber to retrieve the image's color model;@b@ // grabbing a single pixel is usually sufficient@b@ PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);@b@ try {@b@ pg.grabPixels();@b@ } catch (InterruptedException e) {@b@ }@b@ // Get the image's color model@b@ ColorModel cm = pg.getColorModel();@b@ return cm.hasAlpha();@b@ }@b@@b@ public static void main(String[] args) {@b@ CropImageUtil.sourceDirPath="c:\\www";@b@ CropImageUtil.targetDirPath="c:\\www";@b@ CropImageUtil.cropImage("1.jpg", "2.jpg", 280, 70, 450, 318);@b@ }@b@@b@}
运行后,生成2.jpg如下图效果