0

I have a buffered image called bfi1, I want to store this in a new buffered image called bfi2. But bfi2 shouldn't just be a clone, it has to contain multiple rows and columns of the original bfi1.

If am not clear enough ask me any question.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Maarten
  • 635
  • 1
  • 9
  • 29
  • Check this out http://stackoverflow.com/questions/3514158/how-do-you-clone-a-bufferedimage – kaos Jul 30 '13 at 10:41
  • I 'DONT' want to just copy it, I need to multiply it side by side, to the right and down. I am currently using `BufferedImage combo = new BufferedImage(width * 2, height, BufferedImage.TYPE_INT_RGB); Graphics g = combo.getGraphics(); g.drawImage(image, 0, 0, null); g.drawImage(image, width, 0, null);` I can put this in a forloop and some spacing for each. But it isnt very efficient. – Maarten Jul 30 '13 at 11:42
  • So the final image needs to be composed of two original images? – kaos Jul 30 '13 at 13:37
  • Well yeah, but actually allot more. – Maarten Jul 31 '13 at 07:37

2 Answers2

1

Here is a sample code, maybe it will help you. It takes a 500x500 screenshot of a desktop, doubles it and saves it in a file.

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();
    }
}
}
kaos
  • 1,598
  • 11
  • 15
  • This was somewhat what I was looking for, but to basic and actually more complicated then a simple forloop. – Maarten Jul 31 '13 at 10:03
0

It's fixed now.

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(); 
    }
Maarten
  • 635
  • 1
  • 9
  • 29