如何在ANativeWindow上显示JPEG?

时间:2013-08-14 06:47:05

标签: android c android-ndk libjpeg-turbo turbojpeg

我正在尝试使用Android NDK向ANativeWindow显示JPEG。 我通过以下方式获得ANativeWindow*

_window = ANativeWindow_fromSurface(env, surface)

我正在使用libjpeg-turbo加载jpeg:

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);
}

我的问题是,如何在表面上显示已解码的jpeg?我目前正在这样做:

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

但结果(见下文)并不是我所期待的。在将缓冲区发送到表面之前我该怎么办?

snapshot

谢谢

1 个答案:

答案 0 :(得分:2)

只需要使用ANativeWindow_setBuffersGeometry(_window, 0, 0, WINDOW_FORMAT_RGBA_8888)设置ANativeWindow的格式。然后使用TJPF_RGBA格式代替TJPF_ARGB