截图图片成 圆形 图片

/*//截图成圆形
		File localFile = new File("c:/a.jpg");
		BufferedImage sourceImg = ImageIO.read(localFile);
		BufferedImage newImage = new BufferedImage(toWidth, (int)height, originalImage.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(sourceImg, 0, 0, toWidth, (int)height, null);
        g.dispose();
        ByteArrayOutputStream bs = new ByteArrayOutputStream();         
        ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); 
        ImageIO.write(newImage, "png", imOut); 
        */
		BufferedImage bi1 = ImageIO.read(new File("d:/1.jpg"));
        
        // 根据需要是否使用 BufferedImage.TYPE_INT_ARGB
        BufferedImage image = new BufferedImage(bi1.getWidth(), bi1.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
  
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, 110, 98);
        int x = 0, y = 0,srcH=bi1.getHeight(),srcW=bi1.getWidth(),w=110,h=98,srcw,srch;  
        x = srcW / 2 - w / 2;   
        y = srcH / 2 - h / 2;   
        srcw = srcW / 2 + w / 2;   
        srch = srcH / 2 + h / 2; 
        Graphics2D g2 = image.createGraphics();
        image = g2.getDeviceConfiguration().createCompatibleImage(110, 98, Transparency.TRANSLUCENT);
        g2 = image.createGraphics();
        g2.setComposite(AlphaComposite.Clear);
        g2.fill(new Rectangle(110,98));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2.drawImage(bi1, 0, 0,110,98,x,y,srcw,srch, null);
        g2.dispose();
         
        try {
            ImageIO.write(image, "PNG", new File("d:/2.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

  

/*//截图成圆形File localFile = new File("c:/a.jpg");BufferedImage sourceImg = ImageIO.read(localFile);BufferedImage newImage = new BufferedImage(toWidth, (int)height, originalImage.getType());        Graphics g = newImage.getGraphics();        g.drawImage(sourceImg, 0, 0, toWidth, (int)height, null);        g.dispose();        ByteArrayOutputStream bs = new ByteArrayOutputStream();                 ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);         ImageIO.write(newImage, "png", imOut);         */BufferedImage bi1 = ImageIO.read(new File("d:/1.jpg"));                // 根据需要是否使用 BufferedImage.TYPE_INT_ARGB        BufferedImage image = new BufferedImage(bi1.getWidth(), bi1.getHeight(),                BufferedImage.TYPE_INT_ARGB);          Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, 110, 98);        int x = 0, y = 0,srcH=bi1.getHeight(),srcW=bi1.getWidth(),w=110,h=98,srcw,srch;          x = srcW / 2 - w / 2;           y = srcH / 2 - h / 2;           srcw = srcW / 2 + w / 2;           srch = srcH / 2 + h / 2;         Graphics2D g2 = image.createGraphics();        image = g2.getDeviceConfiguration().createCompatibleImage(110, 98, Transparency.TRANSLUCENT);        g2 = image.createGraphics();        g2.setComposite(AlphaComposite.Clear);        g2.fill(new Rectangle(110,98));        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));        g2.setClip(shape);        // 使用 setRenderingHint 设置抗锯齿        g2.drawImage(bi1, 0, 0,110,98,x,y,srcw,srch, null);        g2.dispose();                 try {            ImageIO.write(image, "PNG", new File("d:/2.png"));        } catch (IOException e) {            e.printStackTrace();        }

posted on 2016-01-29 11:25  远方的人  阅读(938)  评论(0编辑  收藏  举报

导航