i am trying to do a live stream camera by doing the following
B4X:
Sub camera_Preview(Data() As Byte)
Dim jpeg() As Byte = camera1.PreviewImageToJpeg(Data, 70)
Log(jpeg.Length)
Dim ins As InputStream
Dim bmp As Bitmap
ins.InitializeFromBytesArray(jpeg, 0, jpeg.Length)
bmp.Initialize2(ins)
camviewimage.Bitmap = bmp
End Sub
but i got a problem when i load the bmp to camviewimage.bitmap the image comes out with a wrong orientation as if it was on landscape mode.
my phone was in portrait mode also my app supportedOrientations is set to portrait
here is a work around for any one have similar issue at least it work for me on my device
i have set a variable oriant to a class global
B4X:
Sub Class_Globals
Private nativeCam As Object
Private cam As Camera
Private r As Reflector
Private target As Object
Private event As String
Public Front As Boolean
Type CameraInfoAndId (CameraInfo As Object, Id As Int)
Type CameraSize (Width As Int, Height As Int)
Private parameters As Object
Private oriant As Int
End Sub
also i have created a sub to capture this integer variable
B4X:
Public Sub getoriantdegree As Int
Return oriant
End Sub
and at last i update the variable oriant in SetDisplayOrientation sub like follwing
B4X:
Private Sub SetDisplayOrientation
r.target = r.GetActivity
r.target = r.RunMethod("getWindowManager")
r.target = r.RunMethod("getDefaultDisplay")
r.target = r.RunMethod("getRotation")
Dim previewResult, result, degrees As Int = r.target * 90
Dim ci As CameraInfoAndId = FindCamera(Front)
r.target = ci.CameraInfo
oriant = r.GetField("orientation")
Dim orientation As Int = r.GetField("orientation")
If Front Then
previewResult = (orientation + degrees) Mod 360
result = previewResult
previewResult = (360 - previewResult) Mod 360
Else
previewResult = (orientation - degrees + 360) Mod 360
result = previewResult
'Log(previewResult)
End If
r.target = nativeCam
r.RunMethod2("setDisplayOrientation", previewResult, "java.lang.int")
r.target = parameters
r.RunMethod2("setRotation", result, "java.lang.int")
CommitParameters
End Sub
at last in on preview sub i use bitmap rotation function that i have found on this forum by calling camera1.getoriantdegree to get the current orientation of the cam
B4X:
Sub RotateImage(obmp As Bitmap, degree As Float) As Bitmap
Dim matrix As JavaObject
matrix.InitializeNewInstance("android.graphics.Matrix", Null)
matrix.RunMethod("postRotate", Array(degree))
Dim bmp As JavaObject
bmp.InitializeStatic("android.graphics.Bitmap")
Dim NewImage As Bitmap = _
bmp.RunMethod("createBitmap", Array(obmp, 0, 0, obmp.Width, obmp.Height, matrix, True))
Return NewImage
End Sub
it working in front and back camera tested on huawei p10
this was a test code oriant variable can be equal any how
B4X:
Dim orientation As Int = r.GetField("orientation")
oriant = orientation