iOS Question ImageToBytes

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All

I am trying to save a barcode image to a file, so I can read it back.

The code below is returning array size = 0.
I also tried the equivalent code for Bitmap instead of B4XBitmap

Any ideas?

B4X:
Dim StringToDisplay as String = "112233445566"

      'This call returns a bitmap as expected. 
 Dim bB4XBitmap As B4XBitmap = CreateCode128Barcode(StringToDisplay)
     'This returns array size = 0
 Dim bImgAsBytes() As Byte = ImageToBytes(bB4XBitmap)
               


Public Sub ImageToBytes(Image As B4XBitmap) As Byte()
   Dim out As OutputStream
   out.InitializeToBytesArray(0)
   Image.WriteToStream(out, 100, "JPEG")
   out.Close
   Return out.ToBytesArray
End Sub

Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
   Dim In As InputStream
   In.InitializeFromBytesArray(bytes, 0, bytes.Length)
#if B4A or B4i
   Dim bmp As Bitmap
   bmp.Initialize2(In)
#else
   Dim bmp As Image
   bmp.Initialize2(In)
#end if
   Return bmp
End Sub
 

iCAB

Well-Known Member
Licensed User
Longtime User
Are you sure that the bitmap is valid? Check its width and height.

Hi Erel,
Thanks for your reply.

The actual code has this block
B4X:
                If ImageView1.IsInitialized Then
                    ImageView1.Bitmap = bB4XBitmap
                End If

The barcode is returned in ImageView1 and get displayed as expected.

Also, just to confirm, I added this
B4X:
Log(bB4XBitmap.Width & " " &  bB4XBitmap.Height)

The log prints: "575 230"

Thanks
iCAB
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
This one step closer. The array now has some bytes, however, the code below doesn't work for some reason.

Please see the comment inside the code

B4X:
                    ImageView1.ContentMode = ImageView1.MODE_FIT
                    ImageView1.Bitmap = CreateCode39(StringToDisplay, ImageView1.Width , (ImageView1.Height/1.3))               

                    OutputImageName = "Code39.png"
                    
                        'Comment the lines below and the image will show the bar code properly
                        'if I Uncomment the code below, the image doesn't display the barcode
                    Dim x As B4XView = ImageView1 'if it is not B4XView
                    
                    Dim bmp As B4XBitmap = x.Snapshot   
                    Dim bImgAsBytes() As Byte = ImageToBytes(bmp)
                    File.WriteBytes(GeneralLib.GL_GetInternalFolder, OutputImageName, bImgAsBytes)
                    
                    Dim bImgBytes1() As Byte = File.ReadBytes(GeneralLib.GL_GetInternalFolder, OutputImageName)
                    Dim bmp1 As B4XBitmap = BytesToImage(bImgBytes1)
                    ImageView1.Bitmap = bmp1
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Please see attached, and look for this comment
''Comment /uncomment to perform the test

Thank you
 

Attachments

  • BitmapToBytes.zip
    4.9 KB · Views: 217
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code works properly here:
B4X:
Sub LoadBarCodeInImage( BarCodeToLoad As String )
    If ImageView1.IsInitialized Then 
        ImageView1.ContentMode = ImageView1.MODE_CENTER
        ImageView1.Bitmap = CreateCode39(BarCodeToLoad, ImageView1.Width , (ImageView1.Height))                
        Dim OutputImageName As String = "Code39.png"
        Dim bImgAsBytes() As Byte = ImageToBytes(ImageView1.Bitmap)
        File.WriteBytes(File.DirLibrary, OutputImageName, bImgAsBytes)
        Dim bImgBytes1() As Byte = File.ReadBytes(File.DirLibrary, OutputImageName)
        Log(bImgBytes1.Length)
        Dim bmp1 As B4XBitmap = BytesToImage(bImgBytes1)
        ImageView1.Bitmap = bmp1
    End If                
End Sub

Doesn't it work for you?
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel

I think between the back and forth, we added this change
B4X:
 Dim bmp As B4XBitmap = x.Snapshot
which caused the sample project code to fail with the newly added CreateCode39

Removing "x.Snapshot" will make it work.


However, if we go back to the original code and use CreateCode128Barcode, you will see the original issue that I reported

B4X:
Sub LoadBarCodeInImage( BarCodeToLoad As String )
    
    
    If ImageView1.IsInitialized Then
        ImageView1.ContentMode = ImageView1.MODE_CENTER
#if CODE_39       
        ImageView1.Bitmap = CreateCode39(BarCodeToLoad, ImageView1.Width , (ImageView1.Height))               
        Dim OutputImageName As String = "Code39.png"
        
        Dim bImgAsBytes() As Byte = ImageToBytes(ImageView1.Bitmap)
        File.WriteBytes(File.DirLibrary, OutputImageName, bImgAsBytes)
        Dim bImgBytes1() As Byte = File.ReadBytes(File.DirLibrary, OutputImageName)
        Log(bImgBytes1.Length)
        Dim bmp1 As B4XBitmap = BytesToImage(bImgBytes1)
        ImageView1.Bitmap = bmp1
        
        
#else
        Dim bB4XBitmap As B4XBitmap = CreateCode128Barcode(BarCodeToLoad)
        Dim OutputImageName As String = "Code128.png"
        
        Dim bImgAsBytes() As Byte = ImageToBytes(bB4XBitmap)
        File.WriteBytes(File.DirLibrary, OutputImageName, bImgAsBytes)
        Dim bImgBytes1() As Byte = File.ReadBytes(File.DirLibrary, OutputImageName)
        Log(bImgBytes1.Length)
        Dim bmp1 As B4XBitmap = BytesToImage(bImgBytes1)
        ImageView1.Bitmap = bmp1
        
#end if       
    End If               
    
    
End Sub



#if B4I
Sub CreateCode128Barcode (Text As String) As B4XBitmap
   Dim filter As NativeObject
   filter = filter.Initialize("CIFilter").RunMethod("filterWithName:", Array("CICode128BarcodeGenerator"))
   filter.RunMethod("setDefaults", Null)
   filter.SetField("inputMessage", filter.ArrayToNSData(Text.GetBytes("UTF8")))
   Dim no As NativeObject = Me
   Return no.RunMethod("convertToUIImage:", Array(filter.GetField("outputImage")))
End Sub
#end if

#if OBJC
- (UIImage*) convertToUIImage:(CIImage*)img {
   return [[UIImage alloc]initWithCIImage:[img imageByApplyingTransform:CGAffineTransformMakeScale(5.0, 5.0)]];
}
#End If


Thanks for all your efforts
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel



Here is the issue. The code below returns a proper image

B4X:
        Dim bB4XBitmap As B4XBitmap = CreateCode128Barcode(BarCodeToLoad)
        Dim OutputImageName As String = "Code128.png"

The code below returns Array size = 0

B4X:
Dim bImgAsBytes() As Byte = ImageToBytes(bB4XBitmap)


Thanks for your reply.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
What is the output of the code I posted? It logs the array size. It didn't print 0 here.

Erel, the code that you posted is all good as I mentioned (problem solved after removing x.snapshot)

The original problem we faced when we tried to save the image generated by CreateCode128Barcode (not code39) to a file.

if you replace LoadBarCodeInImage in the sample project with the one I posted in #9, you will see the problem right away

Thanks
iCAB
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've ran your code on both the simulator and a real device and this is what I see:
B4i_yj4qOAccTj.png


Logs:
Copying updated assets files (1)
Application_Start
76796
Application_Active
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel.

Is it possible to give me quick access to the hosted builder to see if I get the same results as you are getting

I am definitely getting an error with the exact same code. I am using:
B4I = 6.0
Xcode: 10.1

Thanks
iCAB
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel

I updated to the latest B4I (just in case) and I tried with the hosted builder.
I got the same results.

Just in case, I redownload the sample project that I uploaded in post #15, and I ran it.
Got the same results.

I added Log to the code below
B4X:
        Dim bB4XBitmap As B4XBitmap = CreateCode128Barcode(BarCodeToLoad)
        Dim OutputImageName As String = "Code128.png"
        Log("Image Dimensions: " & bB4XBitmap.Width & " " & bB4XBitmap.Height )
        
            'Problem here: this returns an array with 0 bytes
        Dim bImgAsBytes() As Byte = ImageToBytes(bB4XBitmap)
        Log("Array Size: " & bImgAsBytes.Length )
Here is the output
Application_Start
Image Dimensions: 575 230
Array Size: 0

It is not that critical for my application at this point, as I can bypass this code.
We will wait until someone else reports the same issue (unless if you are interested in pursuing this further)

Thanks for all your efforts.
iCAB
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
I have testing on an iPhone 6plus, iOS 12.4.4 I also tested on the simulator XR Version 12.1
 
Upvote 0
Top