How to display a JPEG on ANativeWindow?

3.4k views Asked by At

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

1

There are 1 answers

0
François On BEST ANSWER

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.