CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2014
    Posts
    1

    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.

    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
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Concatenate images - H E L P

    Well, the main thing is to figure out how you'll arrange those images, both within a row, and the rows themselves. What you would do is collect all the rectangles in some list-like container (ArrayList should work just fine), and possibly capture the images into separate objects. You would then create an in-memory bitmap (BufferedImage) with the dimensions:
    width = sum of the widths of all rectangles
    height = max height among the rectangles

    This image would represent one row. Finally, you would then iterate through all the captured images, do some calculations to determine the position within the row, and maybe to center them vertically (for the images that are smaller than the max height), and paint them, one by one, on your row-image. When you're done, you'd then simply discard the individual images, and save the row image to a file.

    Now, when the time comes to create another row, note that the new row's width doesn't have to be the same as the width of the previous row (height is not relevant). You can handle this in two ways - either scale one of the rows to match the other, or scale the "background" so that it matches the wider of the two images.
    In either case, what you would do is - you'd load the existing file (the one you previously saved), and also separately create the new row, using the same process as before. You would then determine the new width for the final image (say, by taking the maximum of the two), and the new height, by adding the two heights together. Then you'd use the same approach: create an in-memory bitmap with those dimensions, and paint the old image on the upper half, and the new row on the bottom half (roughly speaking; you need to determine the exact locations and sizes, of course). When done, overwrite the old file with this newly created image.
    Note that this way you're always adding a single row to the composite image that was created before - it doesn't matter if 3, 5 or 15 rows were already saved, the process is the same.

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

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

    but it didn't work again.
    What do you mean? Could you elaborate a bit?

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured