I am trying to execute the Code128 tutorial according to this website: http://www.onbarcode.com/products/android_barcode/barcodes/code_128.html, but I have no idea how to display the barcode.
Here is the Java code:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView2);
class Test extends View {
public Test(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Code128 barcode = new Code128();
barcode.setData("112233445566");
barcode.setProcessTilde(false);
barcode.setUom(IBarcode.UOM_PIXEL);
barcode.setX(1f);
barcode.setY(75f);
barcode.setLeftMargin(10f);
barcode.setRightMargin(10f);
barcode.setTopMargin(10f);
barcode.setBottomMargin(10f);
barcode.setResolution(72);
barcode.setShowText(true);
barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 12));
barcode.setTextMargin(6);
barcode.setTextColor(AndroidColor.black);
barcode.setForeColor(AndroidColor.black);
barcode.setBackColor(AndroidColor.white);
RectF bounds = new RectF(30, 30, 0, 120);
try {
barcode.drawBarcode(canvas, bounds);
// imageView.setImageBitmap(barcode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
What method should I use to display the barcode?
1)Make that inner view class its own separate class in its own file.
2)Give it all 3 required constructors, not just the one.
3)Add that view to your layout somewhere.
That should be enough. Although if you wanted a true BarcodeView class, you obviously would want to add a setBarcode function that takes a Code128 as a parameter, saves it internally, invalidates the view, and have the onDraw use that barcode function.