不涉及框架纯java实现将图片裁成圆形

时间:2022-03-12 13:10:06

package com.wtsrui.utils;
import java.awt.Color;  
import sun.misc.BASE64Encoder;
import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.RenderingHints;  
import java.awt.geom.Ellipse2D;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;  
import java.net.MalformedURLException;  
import java.net.ProtocolException;  
import java.net.URL;  
 
import javax.imageio.ImageIO;

import com.wtsrui.dao.BSONObject;
import com.wtsrui.dao.Bmob;
import com.wtsrui.test.TestDemo;  
import sun.misc.BASE64Decoder;
public class ToCirclePicture{  
    public static void main1(String[] args) {  
        
        try {  
            
            // 是头像地址  
            // 获取图片的流  
            法一:
             BufferedImage url =  
           getUrlByBufferedImage("某云端自动生成的图片地址");  
            
            法二:获取磁盘上的图片
                    /* BufferedImage src=*/
                    // ImageIO.read(new File("c://20.jpg"));
             int min;//若是长方形图片则按照边短的去裁剪成圆,如果没有这个就会变成椭圆
             if(url.getWidth()>url.getHeight()){
                 min=url.getHeight();
             }else{
                 min=url.getWidth();
             }
        // BufferedImage url = (BufferedImage) src;  
            // 处理图片将其压缩成正方形的小图  
            BufferedImage convertImage = scaleByPercentage(url,100,100);  
            // 裁剪成圆形 (传入的图像必须是正方形的 才会 圆形 如果是长方形的比例则会变成椭圆的)  
            convertImage = convertCircular(url,min);  
            // 生成的图片位置  
            String imagePath = "Imag.png";  
           //File file = new File(imagePath);
            ImageIO.write(convertImage, imagePath.substring(imagePath.lastIndexOf(".") + 1), new File(imagePath));  
            TestDemo.initBmob();
            Bmob.uploadFile(imagePath);
            
            System.out.println("ok");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
 
    /**
     * 缩小Image,此方法返回源图像按给定宽度、高度限制下缩放后的图像
     *  
     * @param inputImage
     * @param maxWidth
     *            :压缩后宽度
     * @param maxHeight
     *            :压缩后高度
     * @throws java.io.IOException
     *             return
     */  
    public static BufferedImage scaleByPercentage(BufferedImage inputImage, int newWidth, int newHeight) throws Exception {  
        // 获取原始图像透明度类型  
        int type = inputImage.getColorModel().getTransparency();  
        int width = inputImage.getWidth();  
        int height = inputImage.getHeight();  
        // 开启抗锯齿  
        RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
        // 使用高质量压缩  
        renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);  
        BufferedImage img = new BufferedImage(newWidth, newHeight, type);  
        Graphics2D graphics2d = img.createGraphics();  
       // graphics2d.setRenderingHints(renderingHints);  
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
        graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);  
        graphics2d.dispose();  
        return img;  
    }  
 
    /**
     * 通过网络获取图片
     *  
     * @param url
     * @return
     */  
    public static BufferedImage getUrlByBufferedImage(String url) {  
        try {  
            URL urlObj = new URL(url);  
            HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();  
            // 连接超时  
            conn.setDoInput(true);  
            conn.setDoOutput(true);  
            conn.setConnectTimeout(25000);  
            // 读取超时 --服务器响应比较慢,增大时间  
            conn.setReadTimeout(25000);  
            conn.setRequestMethod("GET");  
            conn.addRequestProperty("Accept-Language", "zh-cn");  
            conn.addRequestProperty("Content-type", "image/jpeg");  
            conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)");  
            conn.connect();  
            BufferedImage bufImg = ImageIO.read(conn.getInputStream());  
            conn.disconnect();  
            return bufImg;  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (ProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
 
    /**
     * 传入的图像必须是正方形的 才会 圆形 如果是长方形的比例则会变成椭圆的
     *  
     * @param url
     *            用户头像地址
     * @return
     * @throws IOException
     */  
    public static BufferedImage convertCircular(BufferedImage bi1,int min) throws IOException {  
          
//      BufferedImage bi1 = ImageIO.read(new File(url));  
          
        // 这种是黑色底的  
//      BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_INT_RGB);  
 
        // 透明底的图片  
        BufferedImage bi2 = new BufferedImage(min, min, BufferedImage.TYPE_4BYTE_ABGR);  
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0,min,min);  
        Graphics2D g2 = bi2.createGraphics();  
        g2.setClip(shape);  
        // 使用 setRenderingHint 设置抗锯齿  
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(bi1, 0, 0, null);  
        // 设置颜色  
        g2.setBackground(Color.green);  
        g2.dispose();  
        return bi2;  
    }  
    public static String getBase64(String str){
        byte[] b=null;
        String s=null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if(b!=null){
            s=new BASE64Encoder().encode(b);
        }
        return s;
    }
}