简体   繁体   English

如何拍摄自定义形状的屏幕截图?

[英]How to take a custom shaped screenshot?

For creating screenshots in Java i have been using the java.awt.Robot class' createScreenCapture() method. 为了在Java中创建屏幕截图,我一直使用java.awt.Robot类的createScreenCapture()方法。 But i was only able to create screenshots in the Rectangle shape. 但是我只能以Rectangle形状创建屏幕截图。 Now my question is is there any way to take a screenshot of custom shape either by using the Robot class or some other explicit code? 现在我的问题是,是否可以使用Robot类或其他一些显式代码来截取自定义形状的屏幕截图?

And by the way, for custom shape the screenshot has to be transparent and i am likely to store it in PNG format. 顺便说一句,对于自定义形状,屏幕截图必须是透明的,我很可能以PNG格式存储它。

Any answer is appreciated. 任何答案表示赞赏。

is there any way to take a screenshot of custom shape either by using the Robot class or some other explicit code? 是否可以通过使用Robot类或其他一些显式代码来截取自定义形状的屏幕截图?

I like Andrew Thompson's solution that shows how to create a shaped image from a rectangular image. 我喜欢安德鲁·汤普森(Andrew Thompson)的解决方案,该解决方案显示了如何从矩形图像创建形状的图像。 See Cut Out Image in Shape of Text . 请参见以文字形状切出图像

You can do this with any shape. 您可以采用任何形状。 For example you can create your own Polygon by doing something like: 例如,您可以通过执行以下操作来创建自己的多边形:

Polygon polygon = new Polygon();
polygon.addPoint(250, 50);
polygon.addPoint(350, 50);
polygon.addPoint(450, 150);
polygon.addPoint(350, 150);
g.setClip(polygon);
g.drawImage(originalImage, 0, 0, null);

Graphics#setClip(Shape) works fine (as camickr has already suggested): Graphics#setClip(Shape)可以正常工作(如camickr所建议的):

在此处输入图片说明

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ScreenShotClipTest {
  private JComponent makeUI() {
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(new JTree()));
    p.add(new JButton(new AbstractAction("screenshot") {
      @Override public void actionPerformed(ActionEvent e) {
        JButton b = (JButton)e.getSource();
        Window f = SwingUtilities.getWindowAncestor(b);
        try {
          BufferedImage ss = new Robot().createScreenCapture(f.getBounds());
          int w = ss.getWidth(null), h = ss.getHeight(null);
          BufferedImage bi = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
          Graphics g = bi.createGraphics();
          g.setClip(new RoundRectangle2D.Float(0,0,w,h,64,64));
          g.drawImage(ss, 0, 0, null);
          g.dispose();
          ImageIO.write(bi, "png", new File("screenshot.png"));
        } catch(Exception ex) {
          ex.printStackTrace();
        }
      }
    }), BorderLayout.SOUTH);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    final JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new ScreenShotClipTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM