1 | initial version |
This is probably a little late, but may be others can benefit from this answer:
Ok, in the line
//im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
just an empty instance of BufferedImage is created, resulting output of a black image.
The cause of the problems is that depending on the case either a DataBufferedByte or DataBufferInt is created. A look in class Robot's method createScreenCapture() reveals the encoding. A case distinction is needed here, converting a DataBufferInt if necessary. I have added a (very crude) conversion routine to your method above:
public Mat matify(BufferedImage sourceImg) {
long millis = System.currentTimeMillis();
DataBuffer dataBuffer = sourceImg.getRaster().getDataBuffer();
byte[] imgPixels = null;
Mat imgMat = null;
int width = sourceImg.getWidth();
int height = sourceImg.getHeight();
if(dataBuffer instanceof DataBufferByte) {
imgPixels = ((DataBufferByte)dataBuffer).getData();
}
if(dataBuffer instanceof DataBufferInt) {
int byteSize = width * height;
imgPixels = new byte[byteSize*3];
int[] imgIntegerPixels = ((DataBufferInt)dataBuffer).getData();
for(int p = 0; p < byteSize; p++) {
imgPixels[p*3 + 0] = (byte) ((imgIntegerPixels[p] & 0x00FF0000) >> 16);
imgPixels[p*3 + 1] = (byte) ((imgIntegerPixels[p] & 0x0000FF00) >> 8);
imgPixels[p*3 + 2] = (byte) (imgIntegerPixels[p] & 0x000000FF);
}
}
if(imgPixels != null) {
imgMat = new Mat(height, width, CvType.CV_8UC3);
imgMat.put(0, 0, imgPixels);
}
System.out.println("matify exec millis: " + (System.currentTimeMillis() - millis));
return imgMat;
}
The runtime measurement can be omitted obviously.
Greetings