daniel-falk / ujpeg

Jpeg decoder library for micropython
MIT License
8 stars 1 forks source link

ESP32-Cam usage #1

Open e135193 opened 2 years ago

e135193 commented 2 years ago

Hi I wish to use it in my esp32-cam project.

The camera driver does not work properly so I can not get raw data. Therefore I want to use the camera in JPEG mode.

I will have a buf array. In this library we have R G B data separately. How could be the usage in my case ?

spi = SPI(baudrate=40000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
display = Display(spi, dc=Pin(2), cs=Pin(15), rst=Pin(0))

state = camera.init(0, format=camera.RGB565, fb_location=camera.PSRAM)
#state = camera.init(0, format=camera.JPEG, fb_location=camera.PSRAM)
print("Camera State: ",state)
camera.framesize(camera.FRAME_96X96)

while True:
    buf = bytearray(2 * 96 * 96)
    buf = camera.capture()
    display.block(0,0,96,96,buf)
daniel-falk commented 2 years ago

Hi,

I am not familiar with the esp32-cam. The separation of the R, G and B channels are in the decoded output, not the jpeg encoded data. I suspect you can make it work by using the jpeg_decode_from_buffer() function and pointing the raw_image struct to your jpeg encoded buffer. When you want to display the image you might have to shuffle around the order of the pixels to get the format your display requires. If you require an RGB565 image you need to recalculate the values too, the output from the library is 8-bit depth per channel, RGB565 has 5-bit for R and B and 6-bit for G. This can be done as a for-loop over all the pixels, copying from the output buffer to an RGB565 buffer.

If you want to debug while developing the code, I recommend to dump the decoded frame over serial port or some other means to get it to the computer, there you can put it into a ppm file and verify that it looks OK. That way you can isolate any issue to either the decoder or the raw color space conversion.