Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default APP: Concatenate images - H E L P

    Hi guys,


    0 - Introduction:


    Sorry about the grammar. I am not a native english speaker ( i'm brazillian )
    I made a program that print the screen, open it in a frame and allows me to select an area of the image.
    I added a (Save) button that when it is pressed the program write the selected area of image in a new file and save it.


    Java Code:
     
    public class ScreenCaptureRectangle {
    
        Rectangle captureRect;
        Point start = new Point();
        SimpleDateFormat sdf;
    
        ScreenCaptureRectangle(final BufferedImage screen) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
            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);
            JButton btnSave = new JButton("SAVE");
            btnSave.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    double w = captureRect.getWidth();
                    double h = captureRect.getHeight();
                    double x = captureRect.getX();
                    double y = captureRect.getY();
    
                    int W = (int) w;
                    int H = (int) h;
                    int X = (int) x;
                    int Y = (int) y;
    
                    BufferedImage selectImg = screen.getSubimage(X, Y, W, H);
                    try {
                        String fName = generateFileName();
                        if (fName != null) {
                            File f = new File(fName);
                            if (f.createNewFile()) {
                                ImageIO.write(selectImg, "jpg", f);
                            }
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(ScreenCaptureRectangle.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            panel.add(btnSave, BorderLayout.AFTER_LAST_LINE);
    
            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() {
    
                @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);
                }
    
            });
    
            screenLabel.addMouseListener(new MouseAdapter() {
    
                @Override
                public void mousePressed(MouseEvent me) {
                    start = me.getPoint();
                    repaint(screen, screenCopy);
                    selectionLabel.setText("Start Point: " + start);
                    screenLabel.repaint();
                }
    
                @Override
                public void mouseReleased(MouseEvent me) {
                    int endX = me.getX();
                    int endY = me.getY();
                    if (endX > start.x && endY > start.y) {
                        captureRect = new Rectangle(start.x, start.y, endX-start.x, endY-start.y);
                        System.out.println("Rectangle of interest: " + captureRect);
                    }
                }
    
            });
    
            JOptionPane.showMessageDialog(null, panel);
        }
    
        private String generateFileName() {
            return new StringBuilder("screencrop_").append(sdf.format(new Date())).append(".jpg").toString();
        }
    
        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);
                }
            });
        }
    }

    1 - What i want it to do:


    I want to use a list of rectangles to store all the selected images and when i pressed the (OK) button the program will concatenate it all in an single horizontal line (at the same file).
    But i want more than this:
    i want to: Create a new button that when it is pressed it starts to save the nexts rectangles (images) in another list and when i pressed [ (OK) button or this button again ] the program will concatenate this file under the first horizontal line (all at the same file).


    2 - How to to this :


    I think i'll have to compare the heigth of all images of the horizontal line above. For example the first image of the horizontal line below: drawImage(img, 0, biggest_height, null);


    3 - What i did:


    I tried to declare the list on tradicional way but it didnt work because it has limited length. then i tried to use this code:

    ArrayList<Rectangle> al = new ArrayList<Rectangle>();

    al.add(new Rectangle(x, y, w, h));

    but it didn't work again.



    4 - Can you help me to do this app ?


    Thanks for your time.
    And try to help me!!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: APP: Concatenate images - H E L P

    If you want to end with one large image made of several smaller parts, you'll need to define the Image in storage and draw the smaller images into it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: APP: Concatenate images - H E L P

    How can i do this ? can you explain to me ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: APP: Concatenate images - H E L P

    Create a BufferedImage, get a Graphics object for it and use the drawImage() method to draw the smaller images on the larger BufferedImage.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: APP: Concatenate images - H E L P

    but what about the list ? and the buttons ?

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: APP: Concatenate images - H E L P

    You'll have to be more specific than that. What about them?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: APP: Concatenate images - H E L P

    Last edited by Norm; 10-30-2014 at 12:59 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: APP: Concatenate images - H E L P

    Hi All,
    Sorry to bother you again with my project.
    The thing is: i am failing on declare the list and make the condition (with the button ) to paste the subimages.
    I think it is easy to do. But i dont know how and i need this until november.
    So how much you charge to help me ? And how can i pay it ?

    Thanks for yours attention.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: APP: Concatenate images - H E L P

    Why not use a JFrame to show the panel instead of a JOptionPane?

    What problems are you having adding a button to the GUI?
    and a List to the program?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. concatenate command-line arguments as strings
    By Cracked in forum New To Java
    Replies: 1
    Last Post: 12-15-2012, 06:44 PM
  2. how to concatenate two arraylists in a third one
    By nanees in forum New To Java
    Replies: 14
    Last Post: 03-31-2011, 06:08 PM
  3. How to concatenate a number
    By jim01 in forum New To Java
    Replies: 4
    Last Post: 03-27-2011, 04:31 AM
  4. Concatenate year from DATETIME
    By ashin in forum SWT / JFace
    Replies: 11
    Last Post: 07-30-2010, 06:34 PM
  5. Concatenate Linked Lists
    By tttestall in forum New To Java
    Replies: 1
    Last Post: 04-20-2010, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •