Ask Your Question

OpenCV throws exception when calling Mat.put() with direct bytebuffer

asked 2019-07-25 04:15:24 -0500

vinio gravatar image

updated 2019-07-28 14:11:41 -0500

berak gravatar image


I'm getting this java exception in my code using OpenCV SDK for Android :

07-25 09:05:32.258 4431 5373 E ContentValues: byte buffer size = 1228800 07-05 09:03:13.859 7043 7117 E AndroidRuntime: java.lang.UnsupportedOperationException: Provided data element number (1228807) should be multiple of the Mat channels count (3) 07-05 09:03:13.859 7043 7117 E AndroidRuntime: at org.opencv.core.Mat.put(

Mat srcIrImage = new Mat(width * 2,height * 2, CV_8UC3);

srcIrImage.put(0,0, buffer.array()); ==> throws the exception

buffer is a direct byte buffer allocated this way:

buffer = ByteBuffer.allocateDirect(previewRGBAArraySize).order(ByteOrder.LITTLE_ENDIAN);

When using ByteBuffer.allocate it will work but I have a constraint to use direct byte buffers.

May be OpenCV doesn't handle Little-endian as it should. What OpenCV calls data element number 1228807 seems to be the capacity of the buffer. This size is of 1228800 when not using direct buffers. Why is there a difference of 7 bytes here? It seems that OpenCV lib appends these 7 bytes.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-07-30 07:32:05 -0500

vinio gravatar image

As stupid as it can be I solved the issue by using the overloaded version of "put" with offset and size params, so that I can skip the extra 7 (garbage???!!!!!) bytes :

srcIrImage.put(0,0, buffer.array(), 7, imgBufLength);

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-07-25 04:15:24 -0500

Seen: 191 times

Last updated: Jul 25 '19