2017-04-12 47 views
0

請指教。無法將BufferedImage繪製到另一個具有刻度的BufferedImage中

我想繪製輸入BufferedImage輸入到較大的輸出BufferedImage(與縮放)。請看看下面的代碼:

public class Main { 
    public void print(BufferedImage img, int width, int height) { 
     for (int y = 0; y < height; y++) { 
      for (int x = 0; x < width; x++) { 
       System.out.print(img.getRGB(x, y) + " "); 
      } 
      System.out.println(""); 
     } 
    } 

    public static void main(String[] args) { 
     Main app = new Main(); 

     // create input image 
     int inputWidth = 2; 
     int inputHeight = 2; 
     BufferedImage inputImg = new BufferedImage(inputWidth, inputHeight, BufferedImage.TYPE_INT_ARGB); 

     // fill input image 
     for (int y = 0; y < inputHeight; y++) { 
      for (int x = 0; x < inputWidth; x++) { 
       inputImg.setRGB(x, y, y * inputWidth * (1 << 16) + x); 
      } 
     } 

     // print 
     app.print(inputImg, inputWidth, inputHeight); 

     // create output image 
     int outputWidth = 4; 
     int outputHeight = 4; 
     BufferedImage outputImg = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_ARGB); 

     // draw inputImg into outputImg 
     Graphics2D g = outputImg.createGraphics(); 
     g.drawImage(inputImg, 0, 0, outputImg.getWidth(), outputImg.getHeight(), 0, 0, inputImg.getWidth(), inputImg.getHeight(), null); 

     // print 
     app.print(outputImg, outputImg.getWidth(), outputImg.getHeight()); 
    } 
} 

執行將產生以下輸出:

0 1 
131072 131073 
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 

好像Graphics2D對象的作品,因爲我能畫,例如電話線drawLine函數。所以,我認爲inputImg是問題的根源,但我無法弄清楚什麼是錯的。

更新: 我試過使用AffineTransform,但不幸的是它沒有幫助。

Graphics2D g = outputImg.createGraphics(); 
AffineTransform at = new AffineTransform(); 
at.setToIdentity(); 
at.scale(2, 2); 
g.drawImage(inputImg, at, null); 
+0

您是否需要手動縮放圖像? 'Graphcs2D'具有可以執行縮放操作的'AffineTransform' – MadProgrammer

+0

文檔說該版本的drawImage應該在運行[drawImage](https://docs.oracle.com/javase/7/docs/api/java/)上縮放inputImage awt/Graphics.html#drawImage(java.awt.Image,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.awt.image.ImageObserver)) – JavaPupil

+0

Yeah ,這很好,你不能使用'AffineTransform'的任何原因? – MadProgrammer

回答

1

對我來說,這似乎是與你正在使用的顏色計算的一個問題...

當我改變...

inputImg.setRGB(x, y, y * inputWidth * (1 << 16) + x); 

到...

int rgb = y * inputWidth * (1 << 16) + x; 
inputImg.setRGB(x, y, new Color(rgb).getRGB()); 

我得到一個結果,儘管是一個黑點。這表明,我認爲在默認情況下,你的計算產生的0

這是一個alpha值可以在輸出誕生出它們產生:

我的方法產生

-16777216 -16777215 
-16646144 -16646143 

此致產生

0 1 
131072 131073 

現在,坦率地說,這就是爲什麼我不做這種計算,而不是當一個API可用於做fo r我 - 但我是愚蠢的; P

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 

public class Main { 

    public void print(BufferedImage img, int width, int height) { 
     for (int y = 0; y < height; y++) { 
      for (int x = 0; x < width; x++) { 
       System.out.print(img.getRGB(x, y) + " "); 
      } 
      System.out.println(""); 
     } 
    } 

    public static void main(String[] args) { 
     Main app = new Main(); 

     // create input image 
     int inputWidth = 2; 
     int inputHeight = 2; 
     BufferedImage inputImg = new BufferedImage(inputWidth, inputHeight, BufferedImage.TYPE_INT_ARGB); 

     // fill input image 
     System.out.println(inputWidth + "x" + inputHeight); 
     Color color = Color.RED; 
     for (int y = 0; y < inputHeight; y++) { 
      for (int x = 0; x < inputWidth; x++) { 
       int rgb = y * inputWidth * (1 << 16) + x; 
       inputImg.setRGB(x, y, new Color(rgb).getRGB()); 
      } 
     } 

     JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(inputImg))); 

     // print 
     app.print(inputImg, inputWidth, inputHeight); 

     // create output image 
     int outputWidth = 4; 
     int outputHeight = 4; 
     BufferedImage outputImg = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_ARGB); 

     // draw inputImg into outputImg 
     Graphics2D g = outputImg.createGraphics(); 
     g.drawImage(inputImg, 0, 0, outputImg.getWidth(), outputImg.getHeight(), 0, 0, inputImg.getWidth(), inputImg.getHeight(), null); 
     g.dispose(); 
     JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(outputImg))); 

     // print 
     app.print(outputImg, outputImg.getWidth(), outputImg.getHeight()); 
    } 
} 
相關問題