Android Question Camera Bytes to Bitmap

Indy

Active Member
Licensed User
Longtime User
Hi,

Is it possible (or should I say is there a function) to take bytes of a newly taken picture and in memory convert to a bitmap? I already have a function where I do this by saving the image first and then reading it back in as a bitmap before processing, however, it made be wonder whether there was any time to be saved if this can be converted in memory instead thus saving processing time.

Basically, I have a few stages in my function that I need to perform before I get my final image and was looking to reduce/optimise these;

1. Take picture
2. Save picture
3. Read image back in
4. Using EXIF rotate the image to it's correct orientation.
5. Save image back but, resize it before doing so.

I've searched around on the forum and cannot find any posts relating to this.

BTW - I'm using the CamerEx class.
 

MarkusR

Well-Known Member
Licensed User
Longtime User
in picture taken the data is an jpeg.

with the CameraEx class i do not need this step "Using EXIF rotate the image" the orientation in preview and snap is correct.

B4X:
Sub SetPhoto(Data() As Byte)

    Dim inp As InputStream
    inp.InitializeFromBytesArray(Data,0,Data.Length)
 
    Dim B As Bitmap
    B.Initialize2(inp)

    Log("Picture Width:" & B.Width & " Height:" & B.Height)

    AdjustImageView(ImageView1,B)

    'ImageView1.SetBackgroundImage(B)
 
    'ImageView1.Bitmap = B
    'ImageView1.Invalidate

End Sub

Sub AdjustImageView(Imv As ImageView, bmp As Bitmap) 'Vorlage von klaus
 
     'mal meine Formel heraus suchen .. in diashow oder so
 
    Dim Delta, Height, Width As Int
    If bmp.Width / bmp.Height > Imv.Width / Imv.Height Then
        Height = bmp.Height / bmp.Width * Imv.Width
        Delta = (Imv.Height - Height) / 2
        Imv.Height = Height
        Imv.Top = Imv.Top + Delta
    Else
        Width = bmp.Width / bmp.Height * Imv.Height
        Delta = (Imv.Width - Width) / 2
        Imv.Width = Width
        Imv.Left = Imv.Left + Delta
    End If
    Imv.Gravity = Gravity.FILL
    Imv.Bitmap = bmp
End Sub

B4X:
Sub Cam_PictureTaken (Data() As Byte)
   
    CallSubDelayed2(ActivityPhoto, "SetPhoto",Data)

End Sub
 
Last edited:
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi MarkusR,

Thanks for the example code. I managed to use the lines below to read the bytes to bitmap.

Thank you!

in picture taken the data is an jpeg.

with the CameraEx class i do not need this step "Using EXIF rotate the image" the orientation in preview and snap is correct.

B4X:
    Dim inp As InputStream
    inp.InitializeFromBytesArray(Data,0,Data.Length)
 
    Dim B As Bitmap
    B.Initialize2(inp)
 
Upvote 0
Top