Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yuv format is not supported #3

Open
zzxjl1 opened this issue Jan 14, 2020 · 3 comments
Open

Yuv format is not supported #3

zzxjl1 opened this issue Jan 14, 2020 · 3 comments

Comments

@zzxjl1
Copy link

zzxjl1 commented Jan 14, 2020

The problem happens when set the resolution to 2160x1080.

@zzxjl1
Copy link
Author

zzxjl1 commented Jan 14, 2020

`/**

  • Thrown by [YuvImage.Factory.invoke] for non supported yuv formats.
  • According to the Android documentation no phone should encounter this exception.
    */`

I AM.

@Shusek
Copy link

Shusek commented Jan 27, 2020

Hi, this bug can be reproduced by using new CameraX library and if you put setTargetResolution method inside your ImageAnalysis. It it really weird, because if setTargetResolution method is not called image.yuv() works perfectly.

I am really sorry but i do not have many time to investigate that bug so in my code i replace it by a lot slower method, but maybe this snippet from stackoverflow will help you:

fun imageToMat(image: Image): Mat {
var buffer: ByteBuffer
var rowStride: Int
var pixelStride: Int
val width = image.width
val height = image.height
var offset = 0
val planes = image.planes
val data = ByteArray(
image.width * image.height * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8)
val rowData = ByteArray(planes[0].rowStride)
for (i in planes.indices) {
buffer = planes[i].buffer
rowStride = planes[i].rowStride
pixelStride = planes[i].pixelStride
val w = if (i == 0) width else width / 2
val h = if (i == 0) height else height / 2
for (row in 0 until h) {
val bytesPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8
if (pixelStride == bytesPerPixel) {
val length = w * bytesPerPixel
buffer[data, offset, length]
if (h - row != 1) {
buffer.position(buffer.position() + rowStride - length)
}
offset += length
} else {
if (h - row == 1) {
buffer[rowData, 0, width - pixelStride + 1]
} else {
buffer[rowData, 0, rowStride]
}
for (col in 0 until w) {
data[offset++] = rowData[col * pixelStride]
}
}
}
}
val mat = Mat(height + height / 2, width, CvType.CV_8UC1)
mat.put(0, 0, data)
return mat
}

@zzxjl1
Copy link
Author

zzxjl1 commented Jan 28, 2020

try { mat = Yuv.rgb(image); // YUV_420_888 to Mat(RGB) } catch (Exception e) { // destory camera activity and start it with another resolution }
After some debuging, i came up with this method and it works fine now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants