0

我有一个名为 的缓冲图像bfi1,我想将其存储在一个名为 的新缓冲图像中bfi2。但bfi2不应该只是一个克隆,它必须包含原始的多行和多列bfi1

如果不够清楚,请问我任何问题。

4

2 回答 2

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();
    }
}
}
于 2013-07-31T08:51:40.477 回答
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(); 
    }
于 2013-07-31T10:05:09.450 回答