Android Question (Solved) How to convert image to bitmap? (In-Line Java code)

LGS

Member
Licensed User
Longtime User
Hello everyone.
I am using itextpdf to create a pdf, and everything works fine.
In addition to this I need to convert the image to bitmap, so that it is returned in the function.

B4X:
public void draw128(String filename,String ruta) throws IOException, DocumentException {
        
        String configFilePath = ruta;
        Document document = new Document(new Rectangle(340, 842));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(configFilePath + filename));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
          
        Barcode128 uccEan128 = new Barcode128();
        uccEan128.setCodeType(Barcode.CODE128_UCC);
        uccEan128.setCode("(01)00000090311314(10)ABC123(15)060916");

        Image imagetoConvert = uccEan128.createImageWithBarcode(cb, BaseColor.BLACK,BaseColor.BLACK);

        document.add(imagetoConvert);
        document.close(); 'The pdf file is successfully created
          
          // HOW CONVERT imagetoConvert to Bitmap?
          // Bitmap bitmaptoReturn = ???????
          // return bitmaptoReturn;         
}

I appreciate all the support
 

Attachments

  • CB.zip
    10.9 KB · Views: 104

LGS

Member
Licensed User
Longtime User
it looks like @JohanSchoeman is inching you closer šŸ‘. let me just mention some research/testing results i've found:
you can't get the pixel data from an Image created with createImageWithBarcode(). and, for whatever reason, barcode128
does not have a plain createimage() method (pdf417, for example, does). the only type of createimage() method is
createAwtImage(), which is not supported by android.
there are methods relating to Image creation from either an existing Image or from a file, but to create an Image (eg jpeg) from
an existing Image, you have to have its data (bytes) ...

one more thing: when you tried to generate a GS1 barcode with zxing, did you have the GS1 setting on? i never use code128, so i don't know about size ratio's and other properties pertaining to other code types, but i set zxing's GS1 format on and generate a code with your text. is it a GS1?

You are right drgottjr, @JohanSchoeman guided me until I found the solution.
I tried to create the code with zxing, but I could only create standard CODE-128, not GS1, which is a special form of CODE-128.
I appreciate your comments
 
Upvote 0

LGS

Member
Licensed User
Longtime User
Change AppStart code to the below and then get:

View attachment 130281

Change code in AppStart:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
'    MainForm.BackColor = fx.Colors.ARGB(100,0,0,255)
   
    itl.Initialize
   
    Dim mystring As String = "(01)00000090311314(10)ABC123(15)060916"
   
 
    Dim mybytes() As Byte
    mybytes = itl.createPdf(mystring)
    For i = 0 To mybytes.Length - 1
        Log(mybytes(i))
    Next
   
   
    Dim barwidth As Double = 2dip
    Dim lft As Double = 10dip
    For i = 0 To mybytes.Length - 1
        If i Mod 2 = 0 Then
            Canvas1.DrawRect(lft, 10dip, barwidth * mybytes(i), 80dip, fx.Colors.Black, True, 0)
        Else
            Canvas1.DrawRect(lft, 10dip, barwidth * mybytes(i), 80dip, fx.Colors.White, True, 0)
        End If
        lft = lft + barwidth * mybytes(i)
    Next  
   
    Canvas1.DrawText(mystring, Canvas1.Width/2, 110dip, fx.DefaultFont(12), fx.Colors.Black, "CENTER")
   

    Label1.Text = ""
   

   
End Sub

Mr. Schoeman we did...
In B4A, Canvas is different than B4J
Canvas is not a view (like in B4J). It is an object that draws over other views.
With some adjustments to your suggestion I was able to create the image in .png

B4X:
    For i = 0 To mybytes.Length - 1
        If i Mod 2 = 0 Then
            Dim Rect1 As B4XRect
            Rect1.Initialize(lft, 10dip, (barwidth * mybytes(i))+lft, 80dip)
            Canvas1.DrawRect(Rect1,xui.Color_Black,True,1)
'            Canvas1.DrawRect(lft, 10dip, barwidth * mybytes(i), 80dip, Colors.Black, True, 0)
        Else
            Dim Rect2 As B4XRect
            Rect2.Initialize(lft, 10dip, (barwidth * mybytes(i))+lft, 80dip)
            Canvas1.DrawRect(Rect2,xui.Color_White,True,1)
'            Canvas1.DrawRect(lft, 10dip, barwidth * mybytes(i), 80dip, fx.Colors.White, True, 0)
        End If
        lft = lft + (barwidth * mybytes(i))
    Next

I really appreciate your time and support.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Mr. Schoeman we did...
In B4A, Canvas is different than B4J
Canvas is not a view (like in B4J). It is an object that draws over other views.
With some adjustments to your suggestion I was able to create the image in .png

B4X:
    For i = 0 To mybytes.Length - 1
        If i Mod 2 = 0 Then
            Dim Rect1 As B4XRect
            Rect1.Initialize(lft, 10dip, (barwidth * mybytes(i))+lft, 80dip)
            Canvas1.DrawRect(Rect1,xui.Color_Black,True,1)
'            Canvas1.DrawRect(lft, 10dip, barwidth * mybytes(i), 80dip, Colors.Black, True, 0)
        Else
            Dim Rect2 As B4XRect
            Rect2.Initialize(lft, 10dip, (barwidth * mybytes(i))+lft, 80dip)
            Canvas1.DrawRect(Rect2,xui.Color_White,True,1)
'            Canvas1.DrawRect(lft, 10dip, barwidth * mybytes(i), 80dip, fx.Colors.White, True, 0)
        End If
        lft = lft + (barwidth * mybytes(i))
    Next

I really appreciate your time and support.
Glad it is working for you.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
I can scan it with my wrapped ZXing library.
scanning doesn't seem to be the problem, and op didn't exactly answer my question: was the code that i generated with GS1_FORMAT on a GS1? or just a barcode128? i don't know how to tell the difference, and op basically repeated what he had said previously (that he wasn't able to generate a GS1.) he didn't say whether he set GS1_FORMAT on or not. but so long as he is satisfied, that's what counts.
 
Upvote 0
Top