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);
您是否需要手動縮放圖像? 'Graphcs2D'具有可以執行縮放操作的'AffineTransform' – MadProgrammer
文檔說該版本的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
Yeah ,這很好,你不能使用'AffineTransform'的任何原因? – MadProgrammer