Ask Your Question

Revision history [back]

I've solved this by ensuring a buffered image is of type BufferedImage.TYPE_3BYTE_BGR), converting to a byte array, and then copying into the aligned memory in JNI. Code samples below.

Scala part

(Conversion to java should be easy)

// converts an image to BufferedImage.TYPE_3BYTE_BGR
def convertType(img: BufferedImage): BufferedImage = {
    val convertedImg = new BufferedImage(img.getWidth, img.getHeight, BufferedImage.TYPE_3BYTE_BGR)
    convertedImg.getGraphics.drawImage(img, 0, 0, null)
    convertedImg
  }

JNI Part

IplImage* rgbJbyteToIpl(const int width, const int height, const jbyte* const img) {
    int j, channels = 3;
    IplImage * rv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
    // IplImage data is aligned for SIMD instructions.
    // Copy each row individually.
    for (j = 0; j < height; j++) {
        memcpy((void*) &(rv->imageData[(rv->widthStep) * j]), (void*) &(img[j * width * channels]), width * channels);
    }
    return 0;
}