3

I'm trying to add a text over a barcode. In the final stage I want to make the barcode height smaller, and put the text below the barcode, but I'm stuck to add the text. I tried to play with the X, Y beginning, but no luck. What am I missing?

            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try {
                BitMatrix bitMatrix = multiFormatWriter.encode(finalLastID, BarcodeFormat.CODE_128, imageView.getWidth(), imageView.getHeight());
                Bitmap bitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.RGB_565);
                for (int i = 0; i < imageView.getWidth(); i++){
                    for (int j = 0; j < imageView.getHeight()-scriptTextHeight; j++){
                        bitmap.setPixel(i,j,bitMatrix.get(i,j)? Color.BLACK:Color.WHITE);
                    }

                }
                drawStringonBitmap(bitmap,"TEST", 0, 0, Color.RED, 60, 20, false, imageView.getWidth(), imageView.getHeight());
                imageView.setImageBitmap(bitmap);
            } catch (WriterException e) {
                e.printStackTrace();
            }

public static Bitmap drawStringonBitmap(Bitmap src, String string, int beginX, int beginY, int color, int alpha, int size, boolean underline, int width , int height) {

    Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());

    Canvas canvas = new Canvas(result);

    Paint paint = new Paint();
    paint.setColor(color);
    paint.setAlpha(alpha);
    paint.setTextSize(size);
    paint.setAntiAlias(true);
    paint.setUnderlineText(underline);
    canvas.drawBitmap(src, 0, 0, null);
    canvas.drawText(string, beginX, beginY, paint);

    return result;

}
arnoldino
  • 1,969
  • 2
  • 13
  • 15

2 Answers2

2

Seems like a flaw in your code flow, you are using your drawStringOnBitmap() method like a void method, because you are doing nothing with the returned Bitmap, so I guess your code should be sth like :

bitmap = drawStringonBitmap(bitmap,"TEST", 0, 0, Color.RED, 60, 20, false, imageView.getWidth(), imageView.getHeight());

at that point.

Can't test it but I am guessing then :

imageView.setImageBitmap(bitmap);

should set the right bitmap.

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38
  • I changed the function invoke as you described, I think you have a point with that. But the problem is in the drawStringonBitmap() function. I debug the code and I check the "result" bitmap before and after canvas.drawText() and is the same – arnoldino Dec 09 '20 at 18:57
  • Maybe you have to create a new Bitmap with the modified Canvas AFTER you drew the text and then return it – A Honey Bustard Dec 09 '20 at 19:27
0

Create a view of barcode and text below it and take that view as a bitmap just like a screenshot.

public Bitmap getBitmapFromView(View view) {
    view.setDrawingCacheEnabled(true);
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    Bitmap.createScaledBitmap(returnedBitmap, img.getWidth(), img.getHeight(), false);
    return returnedBitmap;
}
ahmad bajwa
  • 966
  • 2
  • 10
  • 30