How to resize the canvas?

Hi,

I am using the following code to scale images. However, I would like

the final image to be of the size 280 x 400 by adding white space

(resizing the canvas). How do I do that?

ImageJ has a very nice plugin to resize the canvas and place the pic

relative to the top left corner, I am looking for something similar.

Thanks

Rauf

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

import javax.swing.*;

publicclass ImageScaler{

finalstaticint IMAGE_TYPE = BufferedImage.TYPE_BYTE_INDEXED;

privatefinalstatic Integer nH =new Integer(400);

privatefinalstatic Integer nW =new Integer(280);

publicstaticvoid main(String[] args)throws IOException{

BufferedImage source = getSourceImage();

BufferedImage scaled = toBufferedImage(getScaledImage(source, nW, nH));

ImageIO.write(scaled,"jpeg",new File("t2_scaled.jpeg"));

}

static BufferedImage getSourceImage(){

try{

BufferedImage image = ImageIO.read(new File("t2.jpg"));

return image;

}catch (IOException io){

System.out.println("Exception caught: " + io);

returnnull;

}

}

publicstatic Image getScaledImage(BufferedImage source,int width,int height){

Integer H =new Integer(source.getHeight());

Integer W =new Integer(source.getWidth());

double scaleFactor = H.doubleValue() / nH.doubleValue();

System.out.println("Scale factor: " + scaleFactor);

if (W.doubleValue() / scaleFactor > nW.doubleValue()){

scaleFactor = W.doubleValue() / nW.doubleValue();

System.out.println("New scale factor: " + scaleFactor);

}

int newWidth = (int) ((double)source.getWidth() / scaleFactor);

int newHeight = (int) ((double)source.getHeight() / scaleFactor);

return source.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

}

publicstatic BufferedImage toBufferedImage(Image image){

new ImageIcon(image);//load image

int w = image.getWidth(null);

int h = image.getHeight(null);

BufferedImage bimage =new BufferedImage(w, h, IMAGE_TYPE);

Graphics2D g = bimage.createGraphics();

g.drawImage(image, 0, 0,null);

g.dispose();

return bimage;

}

}

[4797 byte] By [RaufAa] at [2007-10-2 21:14:05]
# 1

import java.awt.*;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.imageio.stream.ImageInputStream;

import javax.swing.*;

public class ScaleToSize

{

private JPanel getContent(BufferedImage source)

{

final int W = 280;

final int H = 400;

BufferedImage scaled = getScaledImage(source, W, H);

JPanel panel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(5,5,5,5);

gbc.weightx = 1.0;

panel.add(getLabel(source, "source"), gbc);

panel.add(getLabel(scaled, "scaled"), gbc);

return panel;

}

private BufferedImage getScaledImage(BufferedImage in, int w, int h)

{

BufferedImage out = new BufferedImage(w, h, in.getType());

Graphics2D g2 = out.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BICUBIC);

g2.setPaint(UIManager.getColor("Panel.background"));

g2.fillRect(0,0,w,h);

double xScale = (double)w/in.getWidth();

double yScale = (double)h/in.getHeight();

double scale = Math.min(xScale, yScale); // scale to fit

double x = (w - scale*in.getWidth())/2;

double y = (h - scale*in.getHeight())/2;

AffineTransform at = AffineTransform.getTranslateInstance(x,y);

at.scale(scale, scale);

g2.drawRenderedImage(in, at);

g2.dispose();

return out;

}

private JLabel getLabel(BufferedImage image, String title)

{

JLabel label = new JLabel(new ImageIcon(image));

label.setBorder(BorderFactory.createTitledBorder(title));

return label;

}

public static void main(String[] args) throws IOException

{

File file = new File("images/bclynx.jpg");

ImageInputStream iis = ImageIO.createImageInputStream(file);

BufferedImage image = ImageIO.read(iis);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setContentPane(new ScaleToSize().getContent(image));

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

74philipa at 2007-7-14 0:00:37 > top of Java-index,Security,Cryptography...