2016-07-21 28 views
1

有沒有什麼辦法可以在Java中彎曲BufferedImage如何在java中彎曲圖像

我認爲,如果我將圖像裁剪成更小的部分並旋轉它們,那麼我會基本上彎曲圖像,但它似乎不起作用。

這裏是我創建的方法:

/** 
* This is a recursive method that will accept an image the point where the bending will start and the point where the bending will end, as well as the angle of bending 
* 
* @param original:the original image 
* @param startingPoint: the point where the bending should start 
* @param endingPoint: the point where the bending should end 
* @param radiands: the angle 
* @return the bent image 
*/ 
public static BufferedImage getBentImage(BufferedImage original, int startingPoint, int endingPoint, double radians) { 
    if (startingPoint >= endingPoint) 
     return original; 

    int type = BufferedImage.TYPE_INT_ARGB; 
    int width = original.getWidth(); 
    int height = original.getHeight(); 

    BufferedImage crop = original.getSubimage(0, 0, startingPoint, height); 
    BufferedImage crop0 = original.getSubimage(startingPoint, 0, width - startingPoint, height); 
    BufferedImage bendCrop = new BufferedImage(width, height, type); 
    BufferedImage image = new BufferedImage(width, height, type); 

    AffineTransform rotation = new AffineTransform(); 
    rotation.translate(0, 0); 
    rotation.rotate(radians); 

    Graphics2D g = bendCrop.createGraphics(); 
    g.drawImage(crop0, rotation, null); 
    g.dispose(); 

    g = image.createGraphics(); 
    g.drawImage(crop, 0, 0, null); 
    g.drawImage(bendCrop, startingPoint, 0, null); 
    g.dispose(); 

    return getBentImage(image, startingPoint + 1, endingPoint, radians); 
} 

這是原始圖像:

original Image

這是該getBentImage(image, 200, 220, Math.toRadians(1))的結果:

result

我期待的東西更接近:

expected result

任何想法如何真正實現一個getBentImage()方法?

+0

目前還不清楚你實際上「彎曲」的含義。請提供您正在嘗試應用的轉換的準確定義。 –

+0

@AndyTurner難道他們下面的圖片就解釋了嗎? –

+0

不,這就是我問的原因。變形的參數是什麼?起點,終點和弧度參數意味着什麼?例如,你傳入1弧度作爲參數,但沒有任何東西旋轉57度。如果你「彎曲」一個矩形網格,它會是什麼樣子? –

回答

3

作爲評價建議的,一個簡單的方法是將圖像劃分爲3個部分:

  1. 與原始。
  2. 根據彎曲變形彎曲。
  3. 恆定對角線延續。

這是一個快速和有點混亂的例子,顯示原始形狀和它下面的結果形狀。我只是使用圖片的標籤圖標而不是自定義繪畫。 (我也沒有遵守Java命名約定,因爲它是數學變量,而不是典型的編碼。)

由於計算代碼中有很多變量,因此我在最後添加了一個草圖,顯示了什麼變量代表。

enter image description here

public class Main extends JFrame { 

    static BufferedImage image; 

    public static void main(String[] args) { 

     try { 
      image = ImageIO.read(ClassLoader.getSystemResource("img.png")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     new Main(); 
    } 

    public Main() { 

     getContentPane().setLayout(new BorderLayout(5, 10)); 
     BufferedImage img2 = transform(15, 100, 300); 

     JLabel label1 = new JLabel(new ImageIcon(image)); 
     label1.setHorizontalAlignment(JLabel.LEFT); 
     label1.setOpaque(true); 
     label1.setBackground(Color.YELLOW); 
     add(label1, BorderLayout.NORTH); 

     JLabel label2 = new JLabel(new ImageIcon(img2)); 
     label2.setHorizontalAlignment(JLabel.LEFT); 
     label2.setOpaque(true); 
     label2.setBackground(Color.CYAN); 
     add(label2); 

     pack(); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    static BufferedImage transform(int t, int x1, int x2) { 

     final double TH = Math.toRadians(t); 
     final int D = x2 - x1; 
     final int W = image.getWidth(); 
     final int H = image.getHeight(); 

     final int dD = (int) (D/(2 * TH) * Math.sin(2 * TH)); 
     final int dH = (int) (D/TH * Math.pow(Math.sin(TH), 2)); 
     final int pH = (int) ((W - x2) * Math.tan(2 * TH)); 

     final int width = W - (D - dD); 
     final int height = (int) (H + dH + pH); 

     System.out.println(W + " " + H + " -> " + width + " " + height); 

     BufferedImage img2 = new BufferedImage(width, height, image.getType()); 

     for (int x = 0; x < x1; x++) { 
      for (int y = 0; y < H; y++) { 
       int rgb = image.getRGB(x, y); 
       img2.setRGB(x, y, rgb); 
      } 
     } 

     for (int x = x1; x < x2; x++) { 
      for (int y = 0; y < H; y++) { 
       int rgb = image.getRGB(x, y); 
       int dx = (int) (D/(2 * TH) * Math.sin(2 * (x-x1) * TH/D)); 
       int dy = (int) (D/TH * Math.pow(Math.sin((x-x1) * TH/D), 2)); 
       img2.setRGB(x1 + dx, y + dy, rgb); 
      } 
     } 

     for (int x = x2; x < W; x++) { 
      for (int y = 0; y < H; y++) { 
       int rgb = image.getRGB(x, y); 
       int dp = (int) ((x - x2) * Math.tan(2 * TH)); 
       img2.setRGB(x - (D - dD), y + dH + dp, rgb); 
      } 
     } 

     return img2; 
    } 
} 

enter image description here

至於計算,我會離開它給你的功課;它只是在Math.SE上屬於SO的幾何/三角函數。如果你無法弄清楚,我會給你一個方向。

請注意,這種方法可能並不快,當然可以優化,我也會給你。哦,並且粗心大意地將double s修整爲int,所以結果不是像素完美的。

-1

我不知道你通過彎曲的意思,但實際上你有一個矩形,你打破一個片斷它,並將其旋轉:

因此算法如下:

rotate line(x, 0, width-1, 0) 
rotate line(x, height-1, width-1, height-1) 
connect the pieces 

所以基本上你正在尋找旋轉線。

+0

這不會實現連續彎曲。如果你不明白這個問題,請在評論中提問而不是發佈猜測。 – user1803551