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;
}
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayOutputStream;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Image;
....
.....
.....
.....
ByteArrayOutputStream stream=new ByteArrayOutputStream();
linebitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
Image thisImage = Image.getInstance(stream.toByteArray());
cell = new PdfPCell();
cell.setColspan(1);
cell.addElement(thisImage);
table.addCell(cell);
Private Sub Button1_Click
Dim bmp() As Byte = itl.draw128
If bmp = Null Then
Log("NULL")
Else
Log("Data")
End If
End Sub
Public Sub draw128() As Byte()
Dim archivo As String = "Codigo.pdf"
Return nativeMe.RunMethod("draw128", Array(archivo,Main.CarpetaRaiz&"/"))
End Sub
'In-Line Java Code
public byte[] 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)08456789567807(15)080423(10)89B23");
Image imagetoConvert = uccEan128.createImageWithBarcode(cb, BaseColor.BLACK,BaseColor.BLACK);
document.add(imagetoConvert);
document.close();
byte[] data = imagetoConvert.getRawData();
return data;
}
// required imports
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(imageToConvert, "jpg", baos);
baos.flush();
byte[] imageBytes = baos.toByteArray();
baos.close();
return imageBytes;
Public Sub BytesToImage(bytes() As Byte) As Bitmap
Dim In As InputStream
In.InitializeFromBytesArray(bytes, 0, bytes.Length)
Dim bmp As Bitmap
bmp.Initialize2(In)
Return bmp
End Sub
Hello Mr OliverA.You could convert the Image to a byte array (source: https://www.b4x.com/android/forum/t...byte-array-with-resize-quality-options.91746/), return that to B4A and then use B4A to convert the byte array to a Bitmap (source: https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/post-445167)
Java part
Java:// required imports import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(imageToConvert, "jpg", baos); baos.flush(); byte[] imageBytes = baos.toByteArray(); baos.close(); return imageBytes;
Then use the following routine to convert the returned bytes to a Bitmap
B4X:Public Sub BytesToImage(bytes() As Byte) As Bitmap Dim In As InputStream In.InitializeFromBytesArray(bytes, 0, bytes.Length) Dim bmp As Bitmap bmp.Initialize2(In) Return bmp End Sub
Ok I understand.Apologies, the solution would only work in B4J, so please disregard.
2
1
1
2
3
2
4
1
1
1
3
1
2
2
2
1
2
2
2
1
2
2
2
2
2
1
2
2
2
2
2
1
2
2
2
2
2
1
4
1
2
1
2
1
2
3
2
1
1
2
2
1
3
2
1
2
2
2
3
1
2
2
1
3
1
2
1
1
4
1
3
1
1
1
1
3
2
3
1
3
1
1
2
3
1
3
1
3
2
1
1
2
3
2
2
1
1
1
3
1
4
1
3
1
2
1
3
1
4
1
1
1
3
1
1
1
3
2
2
2
1
2
2
2
1
3
2
2
1
2
1
3
1
2
3
1
2
2
1
2
4
1
1
2
2
3
3
1
1
1
2
0
0
0
0
0
0
uccEan128.setCodeType(Barcode.CODE128_UCC);
uccEan128.setCode("(01)00000090311314(10)ABC123(15)060916");
document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE,
BaseColor.BLACK));
String bCode;
if (uccEan128.getCodeType() == uccEan128.CODE128_RAW) {
int idx = uccEan128.getCode().indexOf('\uffff');
if (idx >= 0)
bCode = uccEan128.getCode().substring(0, idx);
else
bCode = uccEan128.getCode();
}
else {
bCode = uccEan128.getRawText(uccEan128.getCode(), uccEan128.getCodeType() == uccEan128.CODE128_UCC);
}
int len = bCode.length();
int fullWidth = (len + 2) * 11 + 2;
byte bars[] = uccEan128.getBarsCode128Raw(bCode);
document.close();
return bars;
Dim mybytes() As Byte
mybytes = itl.createPdf(mystring)
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
#Region Project Attributes
#MainFormWidth: 800
#MainFormHeight: 600
#AdditionalJar: iText-5.0.5
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim itl As itextlib
Private Canvas1 As Canvas
Private Label1 As Label
End Sub
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
Label1.Text = ""
End Sub
'Class module
Sub Class_Globals
Private nativeMe As JavaObject
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
nativeMe = Me
End Sub
Public Sub createPdf(mystring As String) As Byte()
Dim fn As String = "Belinda.pdf"
Dim c128bytes() As Byte
c128bytes = nativeMe.RunMethod("createPdf", Array(fn, mystring))
Return c128bytes
End Sub
#If Java
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.Barcode39;
import com.itextpdf.text.pdf.BarcodeCodabar;
import com.itextpdf.text.pdf.BarcodeDatamatrix;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.BarcodeEANSUPP;
import com.itextpdf.text.pdf.BarcodeInter25;
import com.itextpdf.text.pdf.BarcodePDF417;
import com.itextpdf.text.pdf.BarcodePostnet;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
public byte[] createPdf(String filename, String mystring) throws IOException, DocumentException {
String configFilePath = new File(System.getProperty("user.dir")).getParent() + "/ItextBarcodes/";
// step 1
Document document = new Document(new Rectangle(340, 842));
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(configFilePath + filename));
// step 3
document.open();
document.addHeader("Johan", "My Barcodes");
document.addTitle("Hello Barcodes");
document.addCreationDate();
// step 4
PdfContentByte cb = writer.getDirectContent();
Barcode128 uccEan128 = new Barcode128();
uccEan128.setCodeType(Barcode.CODE128_UCC);
uccEan128.setCode(mystring);
document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE,
BaseColor.BLACK));
String bCode;
if (uccEan128.getCodeType() == uccEan128.CODE128_RAW) {
int idx = uccEan128.getCode().indexOf('\uffff');
if (idx >= 0)
bCode = uccEan128.getCode().substring(0, idx);
else
bCode = uccEan128.getCode();
}
else {
bCode = uccEan128.getRawText(uccEan128.getCode(), uccEan128.getCodeType() == uccEan128.CODE128_UCC);
}
int len = bCode.length();
int fullWidth = (len + 2) * 11 + 2;
byte bars[] = uccEan128.getBarsCode128Raw(bCode);
document.close();
return bars;
}
#End If
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
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?