Android Question Camera EX wrong orientation

PassionDEV

Well-Known Member
Licensed User
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

what i am doing wrong ?
 

PassionDEV

Well-Known Member
Licensed User
this is similar issue my phone is HUAWEI P10 i do not use save to file method i am just viewing in real time

what i understand from the posted code that its rotating the image if it were in landscape mode correct ?

there is no built in resolve in cameraEX itself ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
what i understand from the posted code that its rotating the image if it were in landscape mode correct ?
No. Some cameras use the jpeg metadata to set the orientation instead of actually changing the image orientation.

You need to use the code from that post if you want to load such images correctly.
 
Upvote 0

PassionDEV

Well-Known Member
Licensed User
i will have a problem here this is real time camera app if i save each preview the phone memory will be eatin very fast

as i see i should save the preview to desk to be able to use ExifData
 
Upvote 0

PassionDEV

Well-Known Member
Licensed User
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
 
Upvote 0
Top