`
guanxi
  • 浏览: 40256 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Image Cropper 的 JAVA 支持

 
阅读更多

     image-cropper 是一个很好的 Jquery插件,可以实现图片的选择,旋转,剪切。 可以用做网站头像的处理。

 

            

 

       点击"DONE"按钮,会向后端发送 JSON切割数据,后端的 java代码如下:

package net.watermelon.core.util;

 

 

import java.io.*;  

import java.awt.*;  

import java.awt.image.*;  

import java.awt.Graphics;  

import java.awt.color.ColorSpace;  

 

import javax.imageio.ImageIO;  

 

public class ImageCut {  

     

 

 

 

 

 

 

 

 

 

 

 

public static BufferedImage cut(BufferedImage inputImage,int x,int y,int width,int height,boolean fill){

 

       //获取原始图像透明度类型

 

       int type =BufferedImage.TYPE_INT_RGB;// inputImage.getColorModel().getTransparency();

 

       int w = inputImage.getWidth();

 

       int h = inputImage.getHeight();

 

       int endx=x+width;

 

       int endy=y+height;

 

       if(x>w)

 

           throw new ImagingOpException("起点横坐标超出源图像范围");

 

       if(y>h)

 

           throw new ImagingOpException("起点纵坐标超出源图像范围");

 

       BufferedImage img;

 

       //补白

 

       if(fill){

 

           img = new BufferedImage(width, height, type);

 

           //宽度超出限制

 

           if((w-x)<width){

 

               width=w-x;

 

               endx=w;

 

           }

 

           //高度超出限制

 

           if((h-y)<height){

 

               height=h-y;

 

               endy=h;

 

           }

 

       //不补

 

       }else{

 

           //宽度超出限制

 

           if((w-x)<width){

 

               width=w-x;

 

               endx=w;

 

           }

 

           //高度超出限制

 

           if((h-y)<height){

               height=h-y;

               endy=h;

           }

           img = new BufferedImage(width, height, type); 

       }

      

       

       

       

       

       Graphics2D graphics2d =img.createGraphics();

       

       BufferedImage imageNew = graphics2d.getDeviceConfiguration().createCompatibleImage(width, height,  

                        Transparency.TRANSLUCENT);  

       

       

       graphics2d =imageNew.createGraphics();

       graphics2d.drawImage(inputImage, 0, 0, width, height, x, y, endx, endy, null);

       graphics2d.dispose();

       return imageNew;

   }

 

     

   /** 

    * 缩放图像 

    *  

    * @param srcImageFile       源图像文件地址 

    * @param result             缩放后的图像地址 

    * @param scale              缩放比例 

    * @param flag               缩放选择:true 放大; false 缩小; 

    */  

   public static void scale(String srcImageFile, String result, int scale,  

           boolean flag) {  

       try {  

           BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件  

           int width = src.getWidth(); // 得到源图宽  

           int height = src.getHeight(); // 得到源图长  

           if (flag) {  

               // 放大  

               width = width * scale;  

               height = height * scale;  

           } else {  

               // 缩小  

               width = width / scale;  

               height = height / scale;  

           }  

           Image image = src.getScaledInstance(width, height,Image.SCALE_DEFAULT);  

           BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);  

           Graphics g = tag.getGraphics();  

           g.drawImage(image, 0, 0, null); // 绘制缩小后的图  

           g.dispose();  

           ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流  

       } catch (IOException e) {  

           e.printStackTrace();  

       }  

   }  

     

   /** 

    * 重新生成按指定宽度和高度的图像 

    * @param srcImageFile       源图像文件地址 

    * @param result             新的图像地址 

    * @param _width             设置新的图像宽度 

    * @param _height            设置新的图像高度 

    */  

   public static void scale(String srcImageFile, String result, int _width,int _height) {        

       scale(srcImageFile,result,_width,_height,0,0);  

   }  

     

   public static void scale(String srcImageFile, String result, int _width,int _height,int x,int y) {  

       try {  

             

           BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件  

             

           int width = src.getWidth(); // 得到源图宽  

           int height = src.getHeight(); // 得到源图长  

             

           if (width > _width) {  

                width = _width;  

           }  

           if (height > _height) {  

               height = _height;  

           }             

           Image image = src.getScaledInstance(width, height,Image.SCALE_DEFAULT);  

           BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);  

           Graphics g = tag.getGraphics();  

           g.drawImage(image, x, y, null); // 绘制缩小后的图  

           g.dispose();              

           ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流  

       } catch (IOException e) {  

           e.printStackTrace();  

       }  

   }  

     

   /** 

    * 图像类型转换 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X) 

    */  

   public static void convert(String source, String result) {  

       try {  

           File f = new File(source);  

           f.canRead();  

           f.canWrite();  

           BufferedImage src = ImageIO.read(f);  

           ImageIO.write(src, "JPG", new File(result));  

       } catch (Exception e) {  

           e.printStackTrace();  

       }  

   }  

 

   /** 

    * 彩色转为黑白 

    *  

    * @param source 

    * @param result 

    */  

   public static void gray(String source, String result) {  

       try {  

           BufferedImage src = ImageIO.read(new File(source));  

           ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  

           ColorConvertOp op = new ColorConvertOp(cs, null);  

           src = op.filter(src, null);  

           ImageIO.write(src, "JPEG", new File(result));  

       } catch (IOException e) {  

           e.printStackTrace();  

       }  

   } 

   

   /**

    * 选择任意角度

    * @param bufferedimage

    * @param degree

    * @return

    */

   

   

   public static Rectangle CalcRotatedSize(Rectangle src, int angel) {  

       // if angel is greater than 90 degree, we need to do some conversion  

   angel = Math.abs(angel);

   if (angel >= 90) {  

           if(angel / 90 % 2 == 1){  

               int temp = src.height;  

               src.height = src.width;  

               src.width = temp;  

           }  

           angel = angel % 90;  

       }  

 

       double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;  

       double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;  

       double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;  

       double angel_dalta_width = Math.atan((double) src.height / src.width);  

       double angel_dalta_height = Math.atan((double) src.width / src.height);  

 

       int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha  

               - angel_dalta_width));  

       int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha  

               - angel_dalta_height));  

       int des_width = src.width + len_dalta_width * 2;  

       int des_height = src.height + len_dalta_height * 2;  

       return new java.awt.Rectangle(new Dimension(des_width, des_height));  

   }  

   

   public static BufferedImage Rotate(Image src, int angel) {  

       int src_width = src.getWidth(null);  

       int src_height = src.getHeight(null);  

       // calculate the new image size  

       Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(  

               src_width, src_height)), angel);  

 

       BufferedImage res = null;  

       res = new BufferedImage(rect_des.width, rect_des.height,  

               BufferedImage.TYPE_INT_RGB);  

       Graphics2D g2 = res.createGraphics();

       

       BufferedImage imageNew = g2.getDeviceConfiguration().createCompatibleImage(rect_des.width, rect_des.height,  

                    Transparency.TRANSLUCENT);  

       g2 = imageNew.createGraphics();

       

       // transform  

       g2.translate((rect_des.width - src_width) / 2,  

               (rect_des.height - src_height) / 2);  

       g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);  

       g2.drawImage(src, null, null);  

       return imageNew;  

   }  

   

 

 

   public static void main(String[] argv) throws Exception{

   String srcImageFile="c:\\model2.jpg";

   BufferedImage img =ImageIO.read(new File(srcImageFile)); 

   BufferedImage nimg = ImageCut.Rotate(img,0);

   img = ImageCut.cut(nimg,89,43,101,101,true);

        ImageIO.write(img, "png", new File("c:\\model21.png"));

   }

}  

 

        可以简单地完成头像的切割和保存。

 

     

  • 大小: 136.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics