my landscape images are already as i want them to be at the output, but for the portrait my code does not work, i know i have something to do when the source height > source width but I don't know exactly what. I need help !
Here is my code :
public static BufferedImage resize(BufferedImage img, int newW, int newH){
int w = img.getWidth();
int h = img.getHeight();
double thumbRatio = (double) newW / (double) newH;
int imageWidth = img.getWidth(null);
int imageHeight = img.getHeight(null);
double aspectRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < aspectRatio) {
newH = (int) (newW / aspectRatio);
}
else {
newW = (int) (newH * aspectRatio);
}
//if (w > 1024 || h > 768) {
BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
//}else return img;
}
finally find the solution by adding the code between commentaries "PORTRAIT CASE", this does the trick except that i had to change the final resolution to 1010x740 because of black border apparition with the 1024x768 resoltuion. The solution was to place my image on other white image (jpanel here). Hope this code will be usefull for someone
public static BufferedImage resize(BufferedImage img, int newW, int newH){
int w = img.getWidth();
int h = img.getHeight();
double thumbRatio = (double) newW / (double) newH;
int imageWidth = img.getWidth(null);
int imageHeight = img.getHeight(null);
double aspectRatio = (double) imageWidth / (double) imageHeight;
//PORTRAIT CASE
if (h > w)
{
if (thumbRatio < aspectRatio) {
newH = (int) (newW / aspectRatio);
}
else {
newW = (int) (newH * aspectRatio);
}
BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
JFrame frame = new JFrame();
JLayeredPane lpane = new JLayeredPane();
JPanel panelBlue = new JPanel();
JPanel panelGreen = new JPanel();
frame.setPreferredSize(new Dimension(1024, 768));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
lpane.setBounds(0, 0, 1024, 768);
panelBlue.setBackground(Color.WHITE);
panelBlue.setBounds(0, 0, 1024, 768);
panelBlue.setOpaque(true);
panelGreen.setBackground(Color.GREEN);
panelGreen.setBounds(0, 0, 1024, 768);
panelGreen.setOpaque(true);
lpane.add(panelGreen, new Integer(0), 0);
lpane.add(panelBlue, new Integer(1), 0);
ImageIcon imgg = new ImageIcon(dimg);
JLabel label = new JLabel("", imgg, JLabel.CENTER);
panelBlue.add( label, BorderLayout.CENTER );
frame.pack();
frame.setVisible(true);
BufferedImage bi = new BufferedImage(1010, 740, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
lpane.paint(g2);
g2.dispose();
return bi;
}
//PORTRAIT CASE
if (thumbRatio < aspectRatio) {
newH = (int) (newW / aspectRatio);
}
else {
newW = (int) (newH * aspectRatio);
}
if (w > 1024 || h > 768) {
BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}else return img;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.