Android Question cameraex resize yuvImage

PassionDEV

Active Member
Licensed User
i see this sub in cameraex


B4X:
Public Sub PreviewImageToJpeg(data() As Byte, quality As Int) As Byte()
    Dim size, previewFormat As Object
  
    r.target = parameters
  
    size = r.RunMethod("getPreviewSize")
  
    previewFormat = r.RunMethod("getPreviewFormat")
  
    r.target = size
  
    Dim width = r.GetField("width"), height = r.GetField("height") As Int
  
    Dim yuvImage As Object = r.CreateObject2("android.graphics.YuvImage", _
        Array As Object(data, previewFormat, width, height, Null), _
        Array As String("[B", "java.lang.int", "java.lang.int", "java.lang.int", "[I"))
  
    r.target = yuvImage
  
    Dim rect1 As Rect
  
    rect1.Initialize(0, 0, r.RunMethod("getWidth"), r.RunMethod("getHeight"))
  
    Dim out As OutputStream
  
    out.InitializeToBytesArray(100)
  
    r.RunMethod4("compressToJpeg", Array As Object(rect1, quality, out), _
        Array As String("android.graphics.Rect", "java.lang.int", "java.io.OutputStream"))
      
  
  
    Return out.ToBytesArray
  
  

End Sub
can i set a custom scaled size during the compression to jpeg ? i dont want to convert each frame to bitmap to resize can i do it directly in this function ?
 

PassionDEV

Active Member
Licensed User
1. You can change the preview frames size with CameraEx.SetPreviewSize.

2. Don't process each frame. See the CCTV example: https://www.b4x.com/android/forum/threads/mjpeg-cctv-server.73792/#content
i do similar approach like following

B4X:
Sub camera_Preview(Data() As Byte)
  


If DateTime.Now > lastPreviewSaved + IntervalMs Then


Dim jpeg() As Byte = camera1.PreviewImageToJpeg(Data, 10)

Dim rot As Int = camera1.CurrOrient
Dim ins As InputStream
Dim bmp As Bitmap

ins.InitializeFromBytesArray(jpeg, 0, jpeg.Length)
bmp.Initialize2(ins)
ins.Close

bmp = bmp.Resize(240, 320, False)
bmp = bmp.Rotate(rot)

Dim imagetostring As String
Dim imgbyets() As Byte
imgbyets =  imagetobyets(bmp)
imagetostring = strutls.EncodeBase64(imgbyets)

lastPreviewSaved = DateTime.Now

CallSub2(Service,"LIVECAMSENDER",imagetostring)




End If



End Sub
but the app is freezing alot while doing the resize and rotate process and i want the custom size to set to 240 x 320
 

PassionDEV

Active Member
Licensed User
yes the app is in release mode , oncamera ready i set the preview size to the lowest

B4X:
Sub camera_Ready (Success As Boolean)
If Success Then
camera1.SetJpegQuality(10)

Dim x2, y2, size As Int
For Each PictureSize As CameraSize In camera1.GetSupportedPicturesSizes
Dim TempSize As Int = Sqrt(Power(PictureSize.Width, 2) + Power(PictureSize.Height, 2))
If size = 0 Or TempSize < size Then
x2 = PictureSize.Width
y2 = PictureSize.Height
size = TempSize
End If
Next


Log(x2&": "& y2)
camera1.SetPictureSize(x2,y2)

camera1.CommitParameters
camera1.StartPreview
Else
ToastMessageShow("Cannot open camera.", True)
End If

End Sub

sense some devices will not have the exact size that i need i had to resize the output image but this process is too heavy and make the phone freezes
 

PassionDEV

Active Member
Licensed User
sorry my bad

B4X:
Sub camera_Ready (Success As Boolean)
If Success Then
camera1.SetJpegQuality(10)

Dim x2, y2, size As Int
For Each PictureSize As CameraSize In camera1.GetSupportedPicturesSizes
Dim TempSize As Int = Sqrt(Power(PictureSize.Width, 2) + Power(PictureSize.Height, 2))
If size = 0 Or TempSize < size Then
x2 = PictureSize.Width
y2 = PictureSize.Height
size = TempSize
End If
Next


Log(x2&": "& y2)
camera1.SetPreviewSize(x2,y2)

camera1.CommitParameters
camera1.StartPreview
Else
ToastMessageShow("Cannot open camera.", True)
End If

End Sub
is the preview size need to be a supported size ? or i can set it on any custom size even if its not supported ?
 
Top