Android Question get data YCbCr_420_SP format from Camera1_Preview

joop

Active Member
Licensed User
Longtime User
Hi, I want to get data in YCbCr_420_SP format from the Camera1_Preview event like this :

B4X:
Sub Camera1_Preview (PreviewPic() As Byte)
    If DateTime.Now > lastPreviewSaved + IntervalMs Then
        Dim in As InputStream
        Dim bmp As Bitmap
        in.InitializeFromBytesArray(PreviewPic, 0, PreviewPic.Length)
        Log("Length PrevPicArr=" & PreviewPic.length)
        Log(PreviewPic(2000)) ' is there something in the array ?
        Try
         bmp.Initialize2(in)
         ImageView1.Bitmap = bmp
         Panel2.setbackgroundimage(bmp)
        Catch
        Log("catch " & LastException)
        End Try
        lastPreviewSaved =DateTime.Now
    End If
End Sub


the line bmp.Initialize2(in)" line gives this error in the catch .
java.lang.RuntimeException: Error loading bitmap. Sub

I read this link about this problem,but with try and catch I can't make it work.
https://www.b4x.com/android/forum/t...late-the-camera-preview-frames.20073/#content.

There are negative and positive values in the stream array is this the problem.?
Can a Bitmap be filled with YCbCr format.? bmp.Initialize2(in)
 

JordiCP

Expert
Licensed User
Longtime User
You need to convert the preview data to a different format first. There are different ways to do it.

Try THIS for one of these methods


There are negative and positive values in the stream array is this the problem.?
Can a Bitmap be filled with YCbCr format.? bmp.Initialize2(in)
About your 2 last questions, the negative values aren't so in origin. But as the array holds 8-bit data and the only 8-bit basic type in android is (signed)byte, when you look at their value, they are signed. Better think of them as values ranging from 0x00 to 0xFF
A bitmap needs RGBA888 values when initialized in B4A. You could also used RGB565 with inline java or using existing libraries, but no NV21 YCbCr format
 
Upvote 0

joop

Active Member
Licensed User
Longtime User
Thanks Jordi,! I understand now , I 'll try your library to convert YCbCr_420_SP to RGBA888
and initialize the bitmap.
 
Upvote 0

joop

Active Member
Licensed User
Longtime User
hi Jordi , i used your lib to changed the brightness ( Y luma) for a bitmap.
using the first 307200 bytes.

with this parameters :

camera is always in landscape
myBitmap.Width myBitmap.Height = 640 480
camEx.PreviewHeight camEx.PreviewWidth = 480 640
camEx rotation = 90
this works ok ! .

My camera has these resolutions

Cam Size: 2560x1920
Cam Size: 2560x1536
Cam Size: 2048x1536
Cam Size: 2048x1232
Cam Size: 1600x1200
Cam Size: 1600x960
Cam Size: 800x480
Cam Size: 640x480


How can I change the PreviewHeight en PreviewWidth to a higher resolution en display a
part of this image to the bitmap. (2560x1920)?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Are these resolutions preview resolutions or picture resolutions? (the camera gives you both)

Not sure to understand correctly your question:

> Do you mean that you want to work with the maximum preview resolution, only process a part of the preview data and then convert this part into a bitmap to show it on screen?

> If so, is it always the same "rectangle" and always the same operation or it can vary dinamically?
 
Upvote 0

joop

Active Member
Licensed User
Longtime User
This the show preview routine Sizes

B4X:
Dim csz() As CameraSize

    csz = camEx.GetSupportedPreviewSizes

    For Each cs As CameraSize In csz
      Log("Cam Size: " & cs.Width & "x" & cs.Height)
    Next


Yes that's what I mean , it is always displayed in a rectangle bitmap.
this bitmap has always the same size.


Maybe this isn't possible now I think about it again .o_O
 
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
If what you want is that the bitmap fits the whole screen, just change in the example (in Activity_Create)
B4X:
    'Activity.AddView(myIV,50%X,50%Y,50%X,50%Y) '<-- Comment this line 
    Activity.AddView(myIV,0,0,100%X,100%Y)'50%X,50%Y,50%X,50%Y) '<-- This line instead of the above

The bitmap will be the same but the system will scale it for you (you will see that if you choose scale=1, the quality is good enough. Also, if you must process the preview t is not a good idea to work with very high resolutions)
 
Upvote 0
Top