Java Utililty Methods Image

List of utility methods to do Image

Description

The list of methods to do Image are organized into topic(s).

Method

BufferedImagerenderComponentToImage(JComponent component)
Renders a component into an image, which is useful for playing with the component's resultant image in special effects or transitions
BufferedImage image = createCompatibleImage(component.getWidth(), component.getHeight());
Graphics graphics = image.createGraphics();
component.paint(graphics);
graphics.dispose();
return image;
voidsave(Image image, File file, String formatName)
save
FileOutputStream os = new FileOutputStream(file);
ImageIO.write(toBufferedImage(image), formatName, os);
voidsaveComponentAsImage(final String path, final Component target, final String format)
Given a component, saves it on disk as image.
final JFrame win = (JFrame) SwingUtilities.getWindowAncestor(target);
final Dimension size = win.getSize();
final BufferedImage image = (BufferedImage) win.createImage(size.width, size.height);
final Graphics g = image.getGraphics();
win.paint(g);
g.dispose();
ImageIO.write(image, format, new File(path));
voidSaveImageToFile(Image img, String filename, String outformat)
Save Image To File
BufferedImage bimg = ScaleToSize(img, -1, -1);
if (outformat.equalsIgnoreCase("JPEG") || outformat.equalsIgnoreCase("JPG"))
    SaveJPEG(bimg, new File(filename));
else
    SaveImageToFile(bimg, filename, outformat);
BufferedImagescaleBarcodeMaxWidth(Image i, int width, int cropHeigth)
Scales if Image is wider, Crops if image is higer.
BufferedImage ret;
int newWidth;
int newHeigth;
boolean scale;
boolean crop;
int iWidth = i.getWidth(null);
int iHeigth = i.getHeight(null);
if (iWidth == -1 || iWidth > width) {
...
booleansetImage(Image im, JComponent c)
Ensures that Images is completely loaded
boolean b = true;
MediaTracker mt = new MediaTracker(c);
mt.addImage(im, 0);
try {
    mt.waitForID(0);
} catch (InterruptedException e) {
    System.out.println("Error while image loading."); 
    b = false;
...
voidsetImageDimension(Image imagen)
Establece las dimensiones de una imagen
widthImage = new ImageIcon(imagen).getIconWidth();
heightImage = new ImageIcon(imagen).getIconHeight();
voidshowImage(final String title, final Image image)
show Image
EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        try {
            JFrame jFrame = new JFrame(title);
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.add(new JLabel(new ImageIcon(image)));
            jFrame.pack();
...
voidshowImage(Image image)
show Image
createImageFrame(image).setVisible(true);
voidshowImage(Image image, String title)
Show image in a JFrame.
JFrame frame = new JFrame(title);
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);