Android Question JPEG byte array to Bitmap or BD

canalrun

Well-Known Member
Licensed User
Longtime User
Instead of setting the bitmap with Imageview.Bitmap=, try using Imageview.background=bd. Of course first you need to Dim bd as BitmapDrawable and then bd.Initialize with the JPG you took from camera. Then only set Imageview.background=bd
Inman, May 2, 2011
http://www.b4x.com/android/forum/threads/from-camera-to-imageview.8952/#post-49800

Hello,
I found the previous post from long ago. This is exactly what I want to do, but I'm lost on the bd.initialize step.

I'm using Erel's Camera and CameraEX examples. He has the Sub:

B4X:
Sub Camera1_Preview (PreviewPic() As Byte)
    If DateTime.Now > lastPreviewSaved + IntervalMs Then
        Dim jpeg() As Byte = camEx.PreviewImageToJpeg(PreviewPic, 70)
        lastPreviewSaved = DateTime.Now
        CallSubDelayed2(Communicator, "Send", jpeg)
    End If
End Sub

Basically he ends up with the byte array named jpeg. This is a compressed representation of the bitmap from the camera preview. I plan to send this data to another android device, then I would like to show the JPEG in an ImageView.Bitmap or Panel.Background. I would prefer to use the byte array – not working via files as an interim step.

So you have the byte array jpeg(). Somehow you want to initialize a bitmap or bitmap drawable from the byte array:

bd.initialize(??? what ???)

I have a feeling an input stream from bytes will be in there somewhere, but I just can't seem to make the full connection.

Any help?

Thanks,
Barry.
 

stevel05

Expert
Licensed User
Longtime User
I haven't tried it but InputStream has an InitializeFromBytesArray method, and the BitMap Initialize2 method requires an input stream.

Probably what you're looking for.
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thank you. That's it!

B4X:
Sub Cam1_Preview (Prev() As Byte)
  Dim l As Long = DateTime.Now
 
  If ((l - lastPrev) >= PrevIntMs) Then
    Dim jpeg() As Byte = camEx.PreviewImageToJpeg(Prev, 70)

    Log("Cam1_Preview ms: " & (l - lastPrev) & " Size : " & Prev.Length & " " & jpeg.Length)
   
    Dim ins As InputStream
    Dim bmp As Bitmap
    Dim imgtest As ImageView
   
    ins.InitializeFromBytesArray(jpeg, 0, jpeg.Length)
    bmp.Initialize2(ins)
   
    imgtest.Initialize("")
    imgtest.Gravity = Gravity.FILL
   
    Activity.AddView(imgtest, 10dip, 10dip, 100dip, 100dip)
   
    imgtest.Bitmap = bmp
   
    imgtest.BringToFront

    lastPrev = l + 10000000 ' run once every 10 million msec
  End If
End Sub

When my phone is in portrait mode the JPEG is rotated -90°. When my phone is in landscape mode the JPEG appears right side up. I have seen this issue discussed many times – I will have to research the solution. But, I am very happy to have this working.

Barry.
 
Upvote 0
Top