Would anyone be able to provide an example of using the libwebp NDK library to encode an Image to webp ?
I'm trying to encode an image captured from the camera as RGB_565 using the code below:
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
int w = camera.getParameters().getPreviewSize().width;
int h = camera.getParameters().getPreviewSize().height;
try {
int stride = (w + 1) & ~1;
byte[] out = libwebp.WebPEncodeRGB(data, w, h, stride, 80f);
} catch (Exception e) {
e.printStackTrace();
}
}
I've tried several values for stride, ranging from w, w+1, w*2, w*3 and w*4 ... the latter results in a fatal exception.
The above code produces a black and white image, that duplicates the picture several times all sideways...
I can't tell if WebP supports encoding from a RGB_565 stream, my guess is no.
As an alternative, you can convert it to RGB_888 such as in the answers given here: How does one convert 16-bit RGB565 to 24-bit RGB888?
And then the correct way to do stride is simply width*bytesPerPixel
, or in the case of RGB_888 it would be width * 3
. When encoding RGBA_8888 it would be width * 4
.