我有一个名为bfi1
的缓冲图像,我想将其存储在名为bfi2
的新缓冲图像中。但bfi2
不应只是一个克隆,它必须包含原始bfi1
的多个行和列。
如果不够清楚,请问我任何问题。
答案 0 :(得分:1)
以下是示例代码,也许它会对您有所帮助。它需要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();
}
}
}
答案 1 :(得分:0)
现在已经修好了。
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();
}