Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a JPEG on ANativeWindow?

I am trying to show a JPEG to a ANativeWindow with the Android NDK. I am getting the ANativeWindow* by doing:

_window = ANativeWindow_fromSurface(env, surface)

I am loading the jpeg, with libjpeg-turbo, by doing:

if (tjDecompressHeader2(tj, jpeg, jpegSize, &width, &height, &subsamp) == 0) {
    int format = TJPF_ARGB;
    int pitch  = _windowWidth * tjPixelSize[format];

    _bufferSize = pitch * _windowHeight;
    _buffer = realloc(_buffer, _bufferSize);
    tjDecompress2(tj, jpeg, jpegSize, _buffer, _windowWidth, pitch, _windowHeight, format, 0);
}

My Question is, how to show the decoded jpeg on the surface ? I am currently doing this:

ANativeWindow_Buffer surface_buffer;
if (ANativeWindow_lock(_window, &surface_buffer, NULL) == 0) {
    memcpy(surface_buffer.bits, _buffer,  _bufferSize);
    ANativeWindow_unlockAndPost(_window);
}

But the result (see below) is not what I was expecting. What should I do before sending the buffer to the surface ?

snapshot

Thanks

like image 852
François Avatar asked Oct 21 '25 11:10

François


1 Answers

Just need to set ANativeWindow's format with ANativeWindow_setBuffersGeometry(_window, 0, 0, WINDOW_FORMAT_RGBA_8888). And then use TJPF_RGBA format instead of TJPF_ARGB.

like image 165
François Avatar answered Oct 23 '25 02:10

François



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!