我有一個名爲bfi1
的緩衝圖像,我想將它存儲在名爲bfi2
的新緩衝圖像中。但bfi2
不應該只是一個克隆,它必須包含原始bfi1
的多行和多列。在新的Bufferedimage中以行和列存儲bufferedimage
如果我不夠清楚,問我任何問題。
我有一個名爲bfi1
的緩衝圖像,我想將它存儲在名爲bfi2
的新緩衝圖像中。但bfi2
不應該只是一個克隆,它必須包含原始bfi1
的多行和多列。在新的Bufferedimage中以行和列存儲bufferedimage
如果我不夠清楚,問我任何問題。
這裏是一個示例代碼,也許它會幫助你。它需要桌面的500x500截圖,將其加倍並將其保存在文件中。
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class Test {
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int DOUBLE_HEIGHT = 1000;
public static void main(String[] args) {
try {
Robot robot = new Robot();
Rectangle captureSize = new Rectangle(0, 0, WIDTH, HEIGHT);
BufferedImage image = robot.createScreenCapture(captureSize);
int[] src = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
int[] dst = new int[src.length * 2];
System.arraycopy(src, 0, dst, 0, src.length);
System.arraycopy(src, 0, dst, src.length, src.length);
WritableRaster wr = image.copyData(null).createCompatibleWritableRaster(0, 0, HEIGHT, DOUBLE_HEIGHT);
for (int i = 0; i < wr.getNumBands(); i++) {
wr.setSamples(0, 0, HEIGHT, DOUBLE_HEIGHT, i, dst);
}
BufferedImage doubleImage = new BufferedImage(image.getColorModel(), wr, false, null);
ImageIO.write(doubleImage, "bmp", new File("D:/doubleImage.bmp"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
這有點像我在找的東西,但對於基本的和實際上更復雜的然後是簡單的forloop。 –
現在已經修復。
private void capImage(int rows, int cols,int mleft, int mtop, int mright, int mbot){
glReadBuffer(GL_FRONT);
int width = (int)IMG.getWidth();
int height = (int)IMG.getHeight();
int ix = (int)IMG.getX();
int iy = (int)SCREEN_HEIGHT - ((int)IMG.getY() + height);
int bpp = 4;
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
glReadPixels(ix, iy, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int i = (x + (width * y)) * bpp;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
}
BufferedImage combo = new BufferedImage(1350, 2650, BufferedImage.TYPE_INT_RGB);
Graphics g = combo.getGraphics();
for(int r = 0; r < rows; r ++){
for(int c = 0; c < cols; C++){
if(c != 0&&r == 0){
g.drawImage(image, mleft+(width * c) + (mright * c), mtop, null);
}
else if(c == 0&& r == 0){
g.drawImage(image, mleft, mtop, null);
}
else if(c == 0&& r != 0){
g.drawImage(image, mleft, mtop+(height * r) + (mbot * r), null);
}
else if (c != 0 && r != 0){
g.drawImage(image, mleft + (width * c) + (mright * c), mtop +(height * r) + (mbot * r), null);
}
}
}
try {
ImageIO.write(combo, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
檢查了這一點http://stackoverflow.com/questions/3514158/how-do-you-clone-a-bufferedimage – kaos
我。不要把希望只是複製它,我需要乘以其側一邊,向右和向下。 我目前使用\t \t'BufferedImage combo = new BufferedImage(width * 2,height,BufferedImage.TYPE_INT_RGB); \t \t圖形g = combo.getGraphics(); \t \t g.drawImage(image,0,0,null); \t \t g.drawImage(image,width,0,null);' 我可以把它放在forloop和每個的一些間距。但它不是非常有效。 –
那麼最終的圖像需要由兩張原始圖像組成? – kaos