Skip to Content
NexPDF

Barcodes

Read QR codes, Data Matrix, PDF417 and linear barcodes from PDF pages and images, and draw real vector barcodes, with NexPDF for Android.

English

License module

Reading barcodes needs the conversion module and drawing them the editor module (both Startup tier and above).

Read barcodes from pages

readBarcodesJson(pageIndex, optionsJson) renders a page (every page with -1) and reads the barcodes on it. Positions are in PDF points. An empty string means the call failed; NexPdfJni.lastError() says why.

import com.nexdevit.nexpdf.NexPdfDocument
import com.nexdevit.nexpdf.NexPdfJni
import org.json.JSONArray

NexPdfDocument.openBytes(deliveryNote).use { doc ->
    val codes = JSONArray(doc.readBarcodesJson(-1, null))
    for (i in 0 until codes.length()) {
        val code = codes.getJSONObject(i)
        Log.i("NexPDF", "${code.getInt("page")} ${code.getString("format")} ${code.getString("text")} ${code.getJSONArray("rect")}")
    }
}

Each result has format, text, bytes_base64 (the raw payload of 2D codes, null for linear codes), page, rect ([llx, lly, urx, ury]) and the corner points. Reading renders the pages, so run it off the main thread.

Read barcodes from an image

NexPdfDocument.readBarcodesImageJson(image, optionsJson) (also NexPdfJni.readBarcodesImageJson) reads a PNG or JPEG, for example a camera picture. rect and points are then in pixels with the origin at the top-left, and page is null.

val png = contentResolver.openInputStream(uri)!!.use { it.readBytes() }
val codes = JSONArray(NexPdfDocument.readBarcodesImageJson(png, """{"formats":["qr_code"]}"""))

Options

OptionDefaultMeaning
formatsevery formatLimit the search, for example ["qr_code","code_128"]
dpi200Resolution pages are rendered at
try_hardertrueSpend more time on hard images
pure_barcodefalseThe image holds only one barcode and nothing else
multipletrueReturn every barcode, not just the first
tile_scantrueAlso scan tiles of the page for small codes

Formats: qr_code, data_matrix, aztec, pdf_417, code_128, code_39, code_93, ean_13, ean_8, upc_a, upc_e, itf, codabar and more (MaxiCode and DataBar are also read).

Draw a barcode

addBarcode(pageIndex, llx, lly, urx, ury, format, text) draws the text as a vector barcode, with a white background and a quiet zone, into the rectangle (PDF points). It is real page content, not an image, so it prints sharply at any size. It returns false on failure.

NexPdfDocument.openBytes(label).use { doc ->
    check(doc.addBarcode(0, 400.0, 700.0, 520.0, 820.0, "qr_code", "https://example.com/track/8812")) {
        NexPdfJni.lastError()
    }
    doc.addBarcode(0, 72.0, 60.0, 300.0, 110.0, "code_128", "SHIP-8812-PT")
    doc.addBarcode(0, 320.0, 60.0, 460.0, 110.0, "ean_13", "560123456789")   // check digit computed
    File(filesDir, "label-coded.pdf").writeBytes(doc.saveBytes())
}

Formats you can draw: qr_code, data_matrix, aztec, pdf_417, code_128, code_39, code_93, ean_13 (12 digits plus a computed check digit, or 13), ean_8, upc_a, upc_e, itf, codabar and telepen.

On this page