McAPI - Barcode API Java

Java sample code to create barcodes with the McAPI Barcode REST API service. The sample uses Java 11 LTS but will also work with more recent versions. It will not work with Java 8 or older LTS versions. Jackson is required for building the JSON payload of the request.

Requirements: A free RapidAPI account. Replace YOUR_API_KEY in the code below with your RapidAPI key.

The sample code works with the free tier of the API, see RapidAPI McAPI Barcode Listing for available plans.

See the overview page for a reference that lists all available parameters and error codes.

Create a Barcode using Java with HttpClient module

Shown is the creation of a Code 128 (code type: 7) with Java encoding the text "123456". The module width is set to 0.5mm, the height is set to 20mm, the size of the human readable text under the barcode is set to 10 points. The code is returned as a base64 encoded PDF.

The source code:

// Java

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;

class Barcode {

    public static void main(String[] args) throws IOException, InterruptedException {

    Map<String, Object> json = new HashMap<String, Object>();
    json.put("data", "123456");
    json.put("type", 7);
    json.put("moduleWidth", 0.5);
    json.put("moduleHeight", 20);
    json.put("hrSize", 10);
    json.put("format", "pdf");
    String postData = new ObjectMapper().writeValueAsString(json);

    HttpRequest request = HttpRequest.newBuilder()
        .header("content-type", "application/json")
        .header("x-rapidapi-host", "")
        .header("x-rapidapi-key", "YOUR_API_KEY")
        .method("POST", HttpRequest.BodyPublishers.ofString(postData))

    HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

The data will be delivered as JSON in response.body e.g.:

  "service":"McAPI Barcode Generator,",
  "code":"data:application/pdf;base64,JVBE ... Cg=="

McAPI Barcode API - Sample barcode with Java

The returned code from the Java program:

Image of McAPI Barcode API Sample Barcode Java

Back to McAPI Barcode API main page.