how to crop a scaled image.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sowmya_gopinath
Posted On:   Friday, August 12, 2005 03:39 PM

my objective is to perform crop and scale operations on a image and vice versa. iam able select any portion of original image(marquee tool) and crop it.but after i scale(zoom )that cropped image,i try to select a portion of scaled image and try to crop it.but the selected potion is not cropped.it crops some other portion.it would be nice if readers let me know how to map this scaled image to original image

Re: how to crop a scaled image.

Posted By:   Craig_Wood  
Posted On:   Monday, September 12, 2005 11:54 PM


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

public class CropTest extends JPanel
{
BufferedImage image, orig;
Rectangle clip, loc;
boolean showClip;

public CropTest()
{
loadImage();
clip = new Rectangle(140, 140);
loc = new Rectangle(image.getWidth(), image.getHeight());
showClip = false;
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
if(clip.x == 0)
centerClip(w, h);
loc.x = (w - loc.width)/2;
loc.y = (h - loc.height)/2;
g2.drawImage(image, loc.x, loc.y, this);
if(showClip)
{
g2.setPaint(Color.red);
g2.draw(clip);
}
}

public void setClip(int x, int y)
{
clip.x = x;
clip.y = y;
checkBoundries();
repaint();
}

private void checkBoundries()
{
if(clip.x < loc.x)
clip.x = loc.x;
if(clip.x + clip.width > loc.x + loc.width)
clip.x = loc.x + loc.width - clip.width;
if(clip.y < loc.y)
clip.y = loc.y;
if(clip.y + clip.height > loc.y + loc.height)
clip.y = loc.y + loc.height - clip.height;
}

public Dimension getPreferredSize()
{
return new Dimension(loc.width, loc.height);
}

public void setImage(BufferedImage image)
{
this.image = image;
loc.width = image.getWidth();
loc.height = image.getHeight();
revalidate();
repaint();
}

public void restoreImage()
{
setImage(orig);
}

public BufferedImage getImage() { return image; }

public BufferedImage getOriginalImage() { return orig; }

public Rectangle getClip() { return clip; }

public Rectangle getLocRect() { return loc; }

private void centerClip(int w, int h)
{
int x = (w - clip.width)/2;
int y = (h - clip.height)/2;
clip.setLocation(x, y);
}

private void loadImage()
{
String s = "images/sawwhetowl.jpg";
try
{
URL url = getClass().getResource(s);
orig = ImageIO.read(url);
}
catch(MalformedURLException mue)
{
System.err.println("url: " + mue.getMessage());
}
catch(IOException ioe)
{
System.err.println("read: " + ioe.getMessage());
}
image = orig;
}

private JPanel getClipPanel()
{
// create components
int value = clip.width;
SpinnerNumberModel widthModel = new SpinnerNumberModel(value, 20, 240, 1);
final JSpinner widthSpinner = new JSpinner(widthModel);
value = clip.height;
SpinnerNumberModel heightModel = new SpinnerNumberModel(value, 20, 240, 1);
final JSpinner heightSpinner = new JSpinner(heightModel);
ChangeListener cl = new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
JSpinner spinner = (JSpinner)e.getSource();
int value = ((Integer)spinner.getValue()).intValue();
if(spinner == widthSpinner)
clip.width = value;
if(spinner == heightSpinner)
clip.height = value;
repaint();
}
};
widthSpinner.addChangeListener(cl);
heightSpinner.addChangeListener(cl);
final JCheckBox show = new JCheckBox("show");
show.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showClip = show.isSelected();
repaint();
}
});

// layout components
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("clip"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
gbc.weightx = 1.0;
addComponents(new JLabel("width"), widthSpinner, panel, gbc);
addComponents(new JLabel("height"), heightSpinner, panel, gbc);
gbc.anchor = gbc.CENTER;
panel.add(show, gbc);
return panel;
}

private void addComponents(Component c1, Component c2, Container c,
GridBagConstraints gbc)
{
gbc.anchor = gbc.EAST;
c.add(c1, gbc);
gbc.anchor = gbc.WEST;
c.add(c2, gbc);
}

public static void main(String[] args)
{
CropTest test = new CropTest();
ClipHandler handler = new ClipHandler(test);
ImageHelper helper = new ImageHelper(test);
test.addMouseListener(handler);
test.addMouseMotionListener(handler);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getClipPanel(), "North");
f.getContentPane().add(new JScrollPane(test));
f.getContentPane().add(helper.getControlPanel(), "South");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}

class ClipHandler extends MouseInputAdapter
{
CropTest cropTest;
Point offset;
boolean dragging;

public ClipHandler(CropTest ct)
{
cropTest = ct;
offset = new Point();
dragging = false;
}

public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
Rectangle r = cropTest.getClip();
if(r.contains(p))
{
offset.x = p.x - r.x;
offset.y = p.y - r.y;
dragging = true;
}
}

public void mouseReleased(MouseEvent e)
{
dragging = false;
}

public void mouseDragged(MouseEvent e)
{
if(dragging)
{
int x = e.getX() - offset.x;
int y = e.getY() - offset.y;
cropTest.setClip(x, y);
}
}
}

class ImageHelper
{
CropTest cropTest;
double scale, zoomInc;

public ImageHelper(CropTest ct)
{
cropTest = ct;
scale = 1.0;
zoomInc = 0.01;
}

private void cropImage()
{
BufferedImage image = cropTest.getImage();
Rectangle clip = cropTest.getClip();
Rectangle loc = cropTest.getLocRect();
if(isSafeToClip(clip, loc))
{
int x = clip.x - loc.x;
int y = clip.y - loc.y;
BufferedImage clipped = null;
try
{
clipped = image.getSubimage(x, y, clip.width, clip.height);
}
catch(RasterFormatException rfe)
{
System.out.println("clip out of bounds: " + rfe.getMessage() +
"
x=" + x + ",y=" + y + "," +
"(x + w)=" + (x + clip.width) + "," +
"(x + h)=" + (y + clip.height));
return;
}
cropTest.setImage(clipped);
}
}

private boolean isSafeToClip(Rectangle cr, Rectangle ir)
{
if(cr.x < ir.x || cr.x + cr.width > ir.x + ir.width ||
cr.y < ir.y || cr.y + cr.height > ir.y + ir.height)
return false;
return true;
}

private void scaleImage(int direction)
{
scale += direction * zoomInc;
BufferedImage orig = cropTest.getOriginalImage();
BufferedImage scaled = getScaledImage(orig);
cropTest.setImage(scaled);
}

private BufferedImage getScaledImage(BufferedImage in)
{
int w = (int)(scale * in.getWidth());
int h = (int)(scale * in.getHeight());
BufferedImage scaled = new BufferedImage(w, h, in.getType());
Graphics2D g2 = scaled.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.setPaint(cropTest.getBackground());
g2.fillRect(0,0,w,h);
AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
g2.drawRenderedImage(in, at);
g2.dispose();
return scaled;
}

private void save()
{
BufferedImage image = cropTest.getImage();
try
{
ImageIO.write(image, "jpg", new File("cropTest.jpg"));
}
catch(IOException ioe)
{
System.err.println("ImageHelper write: " + ioe.getMessage());
}
}

public JPanel getControlPanel()
{
JPanel panel = new JPanel(new GridLayout(0,1));
panel.add(getZoomPanel());
panel.add(getImagePanel());
return panel;
}

private JPanel getZoomPanel()
{
final JButton
zoomIn = new JButton("zoom in"),
zoomOut = new JButton("zoom out");
ActionListener l = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
int direction = 0;
if(button == zoomIn)
direction = 1;
if(button == zoomOut)
direction = -1;
scaleImage(direction);
}
};
zoomIn.addActionListener(l);
zoomOut.addActionListener(l);
JPanel panel = new JPanel(new GridLayout(1,0));
panel.add(zoomIn);
panel.add(zoomOut);
return panel;
}

private JPanel getImagePanel()
{
// create components
final JButton
crop = new JButton("crop"),
reset = new JButton("restore"),
save = new JButton("save");
ActionListener l = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == crop)
cropImage();
if(button == reset)
{
scale = 1.0;
cropTest.restoreImage();
}
if(button == save)
save();
}
};
crop.addActionListener(l);
reset.addActionListener(l);
save.addActionListener(l);
// layout
JPanel panel = new JPanel(new GridLayout(1,0));
panel.add(crop);
panel.add(reset);
panel.add(save);
return panel;
}
}
About | Sitemap | Contact