Question

I am developing an app that has to create barcodes and to display them. I am using ZXING to do that and so I can create bitmaps. But the problem is that the bitmap has an arbitrary size in pixels, and it can be very small as well as very large on different devices. How can I solve this problem ?

I could change the size directly using the screen size, but generating a bitmap pixel after pixel takes a lot of time so I have to have a trade-off between computation time and barcode quality.

PS: I thought about converting "dp" in "px", isn't there a better solution? And if not, how to get the ration between those two units?

Was it helpful?

Solution

You want to express your layouts in terms of device-independent pixels ("dip"), yes. That is not the same as pixels. But yes that's exactly what you need to do, to convert from the dip dimension to pixels, and then generate a barcode of that size in pixels. The library of course has no notion of "dip", only "px".

Call getResources().getDisplayMetrics() in your app. The density field on DisplayMetrics is the conversion factor you need. Just multiply dip by this value and round to get the right size in px.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top