java实现gif图片剪裁



<dependency><groupId>javax.media</groupId><artifactId>jai-core</artifactId><version>1.1.3</version></dependency><dependency><groupId>com.sun.media</groupId><artifactId>jai-codec</artifactId><version>1.1.3</version></dependency><!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.10.0</version></dependency>在功能实现之后,作者将自己的修改新建了一个包。如图: 

/*** @param args* @throws Exception*/public static void main(String[] args) throws Exception {try {// 起始坐标,剪切大小int x = 0;int y = 0;int width = 100;int height = 100;// 参考图像大小int clientWidth = 300;int clientHeight = 250;File file = new File("C:\\Users\\tianjingle\\Desktop\\0.gif");BufferedImage image = ImageIO.read(file);double destWidth = image.getWidth();double destHeight = image.getHeight();if(destWidth < width || destHeight < height) {throw new Exception("源图大小小于截取图片大小!");}double widthRatio = destWidth / clientWidth;double heightRatio = destHeight / clientHeight;x = Double.valueOf(x * widthRatio).intValue();y = Double.valueOf(y * heightRatio).intValue();width = Double.valueOf(width * widthRatio).intValue();height = Double.valueOf(height * heightRatio).intValue();System.out.println("裁剪大小 x:" + x + ",y:" + y + ",width:" + width + ",height:" + height);float ratio = ((float) image.getWidth()) / image.getWidth();String formatName = "gif";String pathSuffix = "." + formatName;String pathPrefix = "C:\\Users\\tianjingle\\Desktop\\";String targetPath = pathPrefix + System.currentTimeMillis() + pathSuffix;byte[] target = ImageUtils.cutImage(new FileInputStream(file.getPath()), "gif", x , y , width, height,ratio);FileUtils.writeByteArrayToFile(new File(targetPath),target);} catch (IOException e) {e.printStackTrace();}}

早~
评论
