0

I need to cut the portion of the image selected using mouse. i tried using Robot robot = new Robot(); BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2, w, h));

This cut the images in 72 dpi. My full source code

  public class CropImage extends JFrame implements MouseListener,      MouseMotionListener {

int drag_status = 0, c1, c2, c3, c4;
String homedirectory = System.getProperty("user.dir");

public static void main(String args[]) {
    new CropImage().start();
}

public void start() {
    ImagePanel im = new ImagePanel(homedirectory+"/"+"images"+"/"+"6.jpg");
    add(im);
    setSize(400, 400);
    setVisible(true);
    addMouseListener(this);
    addMouseMotionListener(this);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void draggedScreen() throws Exception {
    int w = c1 - c3;
    int h = c2 - c4;
    w = w * -1;
    h = h * -1;
    Robot robot = new Robot();
    BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2, w, h));
    File save_path = new File("screen1.jpg");
    ImageIO.write(img, "JPG", save_path);
    System.out.println("Cropped image saved successfully.");
   JOptionPane.showMessageDialog(null, "Cropped image saved successfully.");
}

@Override
public void mouseClicked(MouseEvent arg0) {
}

@Override
public void mouseEntered(MouseEvent arg0) {
}

@Override
public void mouseExited(MouseEvent arg0) {
}

@Override
public void mousePressed(MouseEvent arg0) {
    repaint();
    c1 = arg0.getX();
    c2 = arg0.getY();
}

@Override
public void mouseReleased(MouseEvent arg0) {
    repaint();
    if (drag_status == 1) {
        c3 = arg0.getX();
        c4 = arg0.getY();
        try {
            draggedScreen();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Override
public void mouseDragged(MouseEvent arg0) {
    repaint();
    drag_status = 1;
    c3 = arg0.getX();
    c4 = arg0.getY();
}

@Override
public void mouseMoved(MouseEvent arg0) {
}

public void paint(Graphics g) {
    super.paint(g);
    int w = c1 - c3;
    int h = c2 - c4;
    w = w * -1;
    h = h * -1;
    if (w < 0) {
        w = w * -1;
    }
    g.drawRect(c1, c2, w, h);
}

}

class ImagePanel extends JPanel {

private Image img;

public ImagePanel(String img) {
       this(new ImageIcon(img).getImage());
}

public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    // Dimension size = new Dimension(10,10);
    setPreferredSize(size); setMinimumSize(size);
    setMaximumSize(size); setSize(size); setLayout(null);
}

public void paintComponent(Graphics g)
{
    g.drawImage(img, 0, 0, null);
}

}

I need to crop the selected portion in 300 dpi as print ready image. Any idea please suggest.

Dhinakar
  • 4,061
  • 6
  • 36
  • 68

1 Answers1

1

You'll need to resample the image by a factor of 300 / 72d using AffineTransform, as shown here. As you are interpolating, specify the higher quality AffineTransformOp.TYPE_BICUBIC for interpolationType, even though it's slower. Update the image's metatdata, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045