Android Question Problem with image with camera2 example

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is possible that your camera sets the orientation in the jpeg exif header instead of actually rotating the image.

Try this:
1. Save the data to a file: https://www.b4x.com/android/forum/pages/results/?query=file+to+bytes
2. Use the JpegUtils library to read the orientation and rotate the bitmap if needed.
Check this post: https://www.b4x.com/android/forum/t...ibrary-functionality.23801/page-4#post-151355

Note that you don't need to use BitmapExtended library to rotate the bitmap. You can call Bitmap = Bitmap.Rotate instead.
 
Upvote 0

Ramon Bustamante

Member
Licensed User
It is possible that your camera sets the orientation in the jpeg exif header instead of actually rotating the image.

Try this:
1. Save the data to a file: https://www.b4x.com/android/forum/pages/results/?query=file to bytes
2. Use the JpegUtils library to read the orientation and rotate the bitmap if needed.
Check this post: https://www.b4x.com/android/forum/t...ibrary-functionality.23801/page-4#post-151355

Note that you don't need to use BitmapExtended library to rotate the bitmap. You can call Bitmap = Bitmap.Rotate instead.

Thanks Erel. I have followed your instructions saving the image to a file and then using the function "LoadbitmapSample2" (second point).
I always obtain = exifdata1.ORIENTATION_ROTATE_90 '6 keeping the camera horizontal or vertical.
Where is the problem?
 
Upvote 0

Ramon Bustamante

Member
Licensed User
There is no problem. Some devices set the orientation in the header instead of rotating the image. You need to rotate it yourself.
I'm using a Samsung Galaxy A5 2016, Android 7.0. I think that somethin is wrong in TakePictureNow (CamEx2), but I don't know how:

Public Sub TakePictureNow (MyTaskIndex As Int) As ResumableSub
If MyTaskIndex <> TaskIndex Then Return False
Camera.AbortCaptures
Dim builder As JavaObject = Camera.CreateCaptureBuilder
Dim SensorOrientation As Int = GetFromCameraCharacteristic("SENSOR_ORIENTATION")
Dim front As Int = 1
If getIsFrontFacingCamera Then front = -1
Dim orientation As Int = (SensorOrientation + jcamera.GetField("lastKnownOrientation") * front + 360) Mod 360
CaptureSettingMap.Put("JPEG_ORIENTATION", orientation)
SetSettingsFromMap(builder, CaptureSettingMap)
Dim CaptureRequest As Object = Camera.AddCaptureRequest(builder)
If PrintKeys Then PrintAllKeys(CaptureRequest, "Capture Request")
Wait For Camera_PictureTaken (Data() As Byte)
StartPreview(MyTaskIndex, False)
Return Data
End Sub
 
Upvote 0

bl4ck4nt

Member
Licensed User
Longtime User
the problem is the same as mine, every time photo with camera2 orientation always in 90. i try in different angle, orientation result always 90.
Maybe @Ramon Bustamante get problem solving...?
tx...
 
Upvote 0

bl4ck4nt

Member
Licensed User
Longtime User
But when using the built-in camera, the orientation always comes out adjusting with orientation (0, 90, 180, 270) when we photograph, if using camera and camera2 orientation is always at 90.
 
Upvote 0

bl4ck4nt

Member
Licensed User
Longtime User
yes, I use photo orientation for preview in imageview, if using intent camera in photo detail there will be orientation 0, 90, 180, 270 so it can display according to orientation, but if using camera1, camera2 always at 90.
 

Attachments

  • camera2_90.png
    camera2_90.png
    101.6 KB · Views: 310
  • intent_camera.png
    intent_camera.png
    73.6 KB · Views: 271
Upvote 0

b4x-de

Active Member
Licensed User
Longtime User
Dear Ramon,

You might try to compare the orientation of the activity showing the image with the orientation of the image itself. If you have an activity with the Sub TakePicture() from Erel's example try this:

B4X:
Sub TakePicture
 Dim photo AS Bitmap
 Dim bolActivityLandscape As Boolean
 Dim bolPhotoLandscape As Boolean

 Try
  Wait For(cam.FocusAndTakePicture(intTaskIndex)) Complete (Data() As Byte)
  photo = cam.DataToBitmap(Data)

  ' Compare Orientation
  bolActivityLandscape = (Activity.Width > Activity.Height)
  bolPhotoLandscape = (photo.Width > photo.Height)
  If bolActivityLandscape <> bolPhotoLandscape Then
    photo = photo.Rotate(90) ' Rotate if needed
  End If
  ' ...
 Catch
  ' Error handling
 End Try
End Sub

This works for me in most cases very well. For all other cases I added a rotate button to the panel showing the image. This allows the user to rotate the image as needed.

Best wishes,
Thomas
 
Last edited:
Upvote 0
Top