2017-06-15 173 views
0

我的風景圖像已經如我希望他們在輸出,但對於肖像我的代碼不起作用,我知道我當源高度>源寬度時有某事要做,但我不知道具體是什麼。我需要幫助 !如何以橫向適應縱向圖像在縱橫比保持縱橫比

這裏是我的代碼:

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; 
} 

this is the expected output

回答

0

最後加入評論之間的「縱向CASE」的代碼中找到的解決方案,這是卓有成效的,除了我不得不改變最終解決方案1010x740因爲1024x768分辨率的黑色邊框幻影。解決方法是將我的圖像放在其他白色圖像上(jpanel在這裏)。希望這段代碼對某人有用

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; 
}