2012-06-12 13 views
6

का उपयोग कर कैप्चर करने के लिए एक क्षेत्र का चयन करें, मैं जावा आधारित स्क्रीन शॉट एप्लिकेशन बना रहा हूं, और जब आप अपने कीबोर्ड पर कुंजियों के संयोजन को दबाते हैं तो this video ऐसा होता है जहां आप अपना चयन करते हैं और क्षेत्रफल करते हैं स्क्रीन, और यह चयनित क्षेत्र का एक स्क्रीन शॉट लेता है।माउस

माउस का उपयोग करके कैप्चर करने के लिए किसी क्षेत्र का चयन कैसे करें?

+0

मैं सिर्फ कस्टम कर्सर जोड़ने का काम पूरा। मुझे इस बात का कोई अंदाजा नहीं है कि स्क्रीन को चुनने में सक्षम कैसे करें। –

+0

इस प्रश्न को पोस्ट करने के अलावा, इस समस्या को हल करने के आपके प्रयासों में आपने क्या किया है? – Jeffrey

+1

मैं कम से कम 3 दिनों के लिए खोज कर रहा हूं, इस बारे में कुछ संकेतों पर अब यह कैसे कर रहा है। एक नहीं मिला है, इसलिए मैंने इस सवाल को पोस्ट करने का फैसला किया। –

उत्तर

16

प्रारंभ।

Screen Capture Rectangle

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import javax.swing.*; 

/** Getting a Rectangle of interest on the screen. 
Requires the MotivatedEndUser API - sold separately. */ 
public class ScreenCaptureRectangle { 

    Rectangle captureRect; 

    ScreenCaptureRectangle(final BufferedImage screen) { 
     final BufferedImage screenCopy = new BufferedImage(
       screen.getWidth(), 
       screen.getHeight(), 
       screen.getType()); 
     final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy)); 
     JScrollPane screenScroll = new JScrollPane(screenLabel); 

     screenScroll.setPreferredSize(new Dimension(
       (int)(screen.getWidth()/3), 
       (int)(screen.getHeight()/3))); 

     JPanel panel = new JPanel(new BorderLayout()); 
     panel.add(screenScroll, BorderLayout.CENTER); 

     final JLabel selectionLabel = new JLabel(
       "Drag a rectangle in the screen shot!"); 
     panel.add(selectionLabel, BorderLayout.SOUTH); 

     repaint(screen, screenCopy); 
     screenLabel.repaint(); 

     screenLabel.addMouseMotionListener(new MouseMotionAdapter() { 

      Point start = new Point(); 

      @Override 
      public void mouseMoved(MouseEvent me) { 
       start = me.getPoint(); 
       repaint(screen, screenCopy); 
       selectionLabel.setText("Start Point: " + start); 
       screenLabel.repaint(); 
      } 

      @Override 
      public void mouseDragged(MouseEvent me) { 
       Point end = me.getPoint(); 
       captureRect = new Rectangle(start, 
         new Dimension(end.x-start.x, end.y-start.y)); 
       repaint(screen, screenCopy); 
       screenLabel.repaint(); 
       selectionLabel.setText("Rectangle: " + captureRect); 
      } 
     }); 

     JOptionPane.showMessageDialog(null, panel); 

     System.out.println("Rectangle of interest: " + captureRect); 
    } 

    public void repaint(BufferedImage orig, BufferedImage copy) { 
     Graphics2D g = copy.createGraphics(); 
     g.drawImage(orig,0,0, null); 
     if (captureRect!=null) { 
      g.setColor(Color.RED); 
      g.draw(captureRect); 
      g.setColor(new Color(255,255,255,150)); 
      g.fill(captureRect); 
     } 
     g.dispose(); 
    } 

    public static void main(String[] args) throws Exception { 
     Robot robot = new Robot(); 
     final Dimension screenSize = Toolkit.getDefaultToolkit(). 
       getScreenSize(); 
     final BufferedImage screen = robot.createScreenCapture(
       new Rectangle(screenSize)); 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new ScreenCaptureRectangle(screen); 
      } 
     }); 
    } 
} 
+0

वाह बहुत बहुत धन्यवाद! बहुत उपयोगी बस सोच रहा था कि तुमने ऐसा किया? –

+0

ठीक है मैंने इसे स्वीकार कर लिया। फिर से धन्यवाद आदमी। –

+0

यह केवल तभी काम करेगा यदि आप जावा एप्लिकेशन के स्क्रीनशॉट को कैप्चर कर रहे हैं। आप उपयोगकर्ता के डेस्कटॉप पर चल रहे अन्य एप्लिकेशन को पकड़ने में सक्षम नहीं होंगे। मैंने सोचा था कि आप एक सामान्य उद्देश्य स्क्रीन कैप्चर ऐप चाहते थे जिसका मतलब है कि आप स्विंग का उपयोग नहीं कर सकते हैं। – chubbsondubs

2

यह सुंदर करने के लिए सरल है:

कुछ इस तरह के साथ

http://www.javalobby.org/forums/thread.jspa?threadID=16400&tstart=0

http://www.javaworld.com/javaworld/jw-04-2006/jw-0424-funandgames.html

+0

मुझे पता है कि स्क्रीन शॉट कैसे लेना और सहेजना है। मैं नहीं चाहता कि आपके माउस के साथ एक विशिष्ट क्षेत्र का चयन कैसे करें। –

+0

दूसरा लिंक विवरण बताता है कि यह कैसे काम कर सकता है। – chubbsondubs

संबंधित मुद्दे