Recipe files
LICENSE-libzueci 1569 bytes · raw
libzueci (core/src/libzueci of zxing-cpp), an open source UTF-8 ECI library adapted from libzint
Copyright (C) 2022 gitlost
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided
with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
LICENSE-zint 1535 bytes · raw
libzint - the open source barcode library
Copyright (C) 2008-2025 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
android/BarcodeWriter.kt 3559 bytes · raw
// zxingcpp.BarcodeWriter: the writer of zxing-cpp's new backend for Android, from the otok recipe of zxing-cpp; its
// native side (ZXingCppWriter.cpp) is in libzxingcpp_android.so with the reader of zxing-cpp's Android wrapper.
// The names follow the writer of zxing-cpp's Kotlin/Native wrapper (wrappers/kn).
// SPDX-License-Identifier: CC0-1.0
package zxingcpp
import android.graphics.Bitmap
public class BarcodeWritingException(message: String?) : Exception("Failed to write barcode: $message")
// What to create: the format, one of BarcodeReader.Format that zxing-cpp can create, and options of that format
// as zxing-cpp's CreatorOptions takes them: "key=value" pairs separated by commas, or a JSON object
// ("ecLevel=30%, gs1"; ecLevel, eci, gs1, readerInit, forceSquare, columns, rows, version, dataMask).
public data class CreatorOptions @JvmOverloads constructor(
var format: BarcodeReader.Format,
var options: String = "",
)
// How to draw it: scale, the size of a module in pixels (negative: the barcode fits abs(scale) pixels as close as
// possible); rotate, by 0, 90, 180 or 270 degrees; addHRT, the human readable text below a linear barcode;
// addQuietZones, the margins the format requires.
public data class WriterOptions @JvmOverloads constructor(
var scale: Int = 1,
var rotate: Int = 0,
var addHRT: Boolean = false,
var addQuietZones: Boolean = true,
)
public object BarcodeWriter {
init {
System.loadLibrary("zxingcpp_android")
}
// A barcode of the bytes, as an opaque ARGB_8888 bitmap (black modules on white).
@JvmStatic
@JvmOverloads
public fun write(bytes: ByteArray, creator: CreatorOptions, writer: WriterOptions = WriterOptions()): Bitmap =
writeBitmap(bytes, false, creator, writer)
// A barcode of the text (encoded as UTF-8, with the ECI the format needs for it).
@JvmStatic
@JvmOverloads
public fun write(text: String, creator: CreatorOptions, writer: WriterOptions = WriterOptions()): Bitmap =
writeBitmap(text.toByteArray(Charsets.UTF_8), true, creator, writer)
// The barcode of the bytes as an SVG document.
@JvmStatic
@JvmOverloads
public fun writeSvg(bytes: ByteArray, creator: CreatorOptions, writer: WriterOptions = WriterOptions()): String =
writeSvg(bytes, false, creator, writer)
// The barcode of the text as an SVG document.
@JvmStatic
@JvmOverloads
public fun writeSvg(text: String, creator: CreatorOptions, writer: WriterOptions = WriterOptions()): String =
writeSvg(text.toByteArray(Charsets.UTF_8), true, creator, writer)
private fun writeBitmap(contents: ByteArray, text: Boolean, creator: CreatorOptions, writer: WriterOptions): Bitmap =
nativeWriteBitmap(contents, text, creator.format.value, creator.options,
writer.scale, writer.rotate, writer.addHRT, writer.addQuietZones)
private fun writeSvg(contents: ByteArray, text: Boolean, creator: CreatorOptions, writer: WriterOptions): String =
String(nativeWriteSvg(contents, text, creator.format.value, creator.options,
writer.scale, writer.rotate, writer.addHRT, writer.addQuietZones), Charsets.UTF_8)
// the parameters as values: the native side reads no Kotlin objects; failures throw BarcodeWritingException
private external fun nativeWriteBitmap(
contents: ByteArray, text: Boolean, format: Int, options: String,
scale: Int, rotate: Int, addHRT: Boolean, addQuietZones: Boolean,
): Bitmap
private external fun nativeWriteSvg(
contents: ByteArray, text: Boolean, format: Int, options: String,
scale: Int, rotate: Int, addHRT: Boolean, addQuietZones: Boolean,
): ByteArray
}
android/CMakeLists.txt 1487 bytes · raw
# libzxingcpp_android.so as zxing-cpp's Android wrapper builds it (wrappers/android/zxingcpp/src/main/cpp/CMakeLists.txt
# of ZXING_SOURCE, the source of zxing-cpp), and with ZXING_WRITERS = NEW also the writer of the otok recipe
# (ZXingCppWriter.cpp, the native side of BarcodeWriter.kt): one library, so the app carries one copy of zxing-cpp.
# Adapted from that file of zxing-cpp (wrappers/android/zxingcpp/src/main/cpp/CMakeLists.txt, the zxing-cpp authors,
# Apache License 2.0). Changed: core/ and ZXingCpp.cpp taken from ZXING_SOURCE instead of the paths of the wrapper's
# directory, ZXING_WRITERS settable by the recipe, and ZXingCppWriter.cpp added for ZXING_WRITERS = NEW.
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.16)
project(ZXingCppAndroid)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(ZXING_READERS ON)
if (NOT DEFINED ZXING_WRITERS)
set(ZXING_WRITERS OFF)
endif()
set(ZXING_EXPERIMENTAL_API ON CACHE BOOL "Enable ZXing experimental API (hardcoded for this wrapper)")
add_subdirectory(${ZXING_SOURCE}/core ZXing EXCLUDE_FROM_ALL)
add_library(zxingcpp_android SHARED ${ZXING_SOURCE}/wrappers/android/zxingcpp/src/main/cpp/ZXingCpp.cpp)
if (ZXING_WRITERS STREQUAL "NEW")
target_sources(zxingcpp_android PRIVATE ZXingCppWriter.cpp)
endif()
target_link_options(zxingcpp_android PRIVATE "-Wl,-z,max-page-size=16384")
target_link_libraries(zxingcpp_android PRIVATE ZXing::ZXing log jnigraphics)
android/ZXingCppWriter.cpp 5683 bytes · raw
// The native side of zxingcpp.BarcodeWriter (BarcodeWriter.kt): the writer of zxing-cpp's new backend
// (CreateBarcode.h, WriteBarcode.h) for Android, built by the otok recipe of zxing-cpp into libzxingcpp_android.so
// beside the reader of zxing-cpp's Android wrapper (CMakeLists.txt).
// SPDX-License-Identifier: CC0-1.0
#include "CreateBarcode.h"
#include "WriteBarcode.h"
#include <android/bitmap.h>
#include <jni.h>
#include <cstdint>
#include <exception>
#include <stdexcept>
#include <string>
using namespace ZXing;
namespace {
// zxingcpp.BarcodeWritingException with the message, unless a Java exception is already pending
void throw_writing_exception(JNIEnv* env, char const* message)
{
if (env->ExceptionCheck()) return;
if (jclass const c = env->FindClass("zxingcpp/BarcodeWritingException")) env->ThrowNew(c, message);
}
// a Java exception thrown by a JNI call below ends the call through this, the C++ one is turned into a Java one
struct java_exception
{
};
void check(JNIEnv* env)
{
if (env->ExceptionCheck()) throw java_exception{};
}
std::string string_of(JNIEnv* env, jstring s)
{
if (s == nullptr) return {};
// the options are ASCII ("ecLevel=30%, gs1"), in which modified UTF-8 is UTF-8
char const* const chars = env->GetStringUTFChars(s, nullptr);
check(env);
std::string r{chars};
env->ReleaseStringUTFChars(s, chars);
return r;
}
// contents: the bytes to encode, or with text its UTF-8 (Kotlin encodes it: JNI strings are modified UTF-8)
Barcode create(JNIEnv* env, jbyteArray contents, jboolean text, jint format, jstring options)
{
CreatorOptions creator{static_cast<BarcodeFormat>(format), string_of(env, options)};
jsize const size = env->GetArrayLength(contents);
std::string data(static_cast<std::size_t>(size), '\0');
env->GetByteArrayRegion(contents, 0, size, reinterpret_cast<jbyte*>(data.data()));
check(env);
return text ? CreateBarcodeFromText(data, creator) : CreateBarcodeFromBytes(data.data(), size, creator);
}
WriterOptions writer_options(jint scale, jint rotate, jboolean add_hrt, jboolean add_quiet_zones)
{
return WriterOptions().scale(scale).rotate(rotate).addHRT(add_hrt).addQuietZones(add_quiet_zones);
}
// an ARGB_8888 bitmap of the image the writer made, opaque: a gray level per pixel
jobject bitmap_of(JNIEnv* env, Image const& image)
{
if (image.format() != ImageFormat::Lum) throw std::runtime_error("the writer made an image that is not grayscale");
jclass const config_class = env->FindClass("android/graphics/Bitmap$Config");
check(env);
jfieldID const argb = env->GetStaticFieldID(config_class, "ARGB_8888", "Landroid/graphics/Bitmap$Config;");
check(env);
jobject const config = env->GetStaticObjectField(config_class, argb);
jclass const bitmap_class = env->FindClass("android/graphics/Bitmap");
check(env);
jmethodID const create_bitmap =
env->GetStaticMethodID(bitmap_class, "createBitmap", "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
check(env);
jobject const bitmap = env->CallStaticObjectMethod(bitmap_class, create_bitmap, image.width(), image.height(), config);
check(env);
AndroidBitmapInfo info;
void* pixels = nullptr;
if (AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS
|| AndroidBitmap_lockPixels(env, bitmap, &pixels) != ANDROID_BITMAP_RESULT_SUCCESS) {
throw std::runtime_error("cannot write the pixels of the bitmap");
}
for (int y = 0; y < image.height(); ++y) {
auto* const row = reinterpret_cast<std::uint32_t*>(static_cast<std::uint8_t*>(pixels) + y * info.stride);
for (int x = 0; x < image.width(); ++x) {
std::uint32_t const gray = *image.data(x, y);
row[x] = 0xff000000u | gray << 16 | gray << 8 | gray; // the byte order of R, G and B does not matter
}
}
AndroidBitmap_unlockPixels(env, bitmap);
return bitmap;
}
template <typename F>
auto guarded(JNIEnv* env, F f) -> decltype(f())
{
try {
return f();
} catch (java_exception const&) {
} catch (std::exception const& e) {
throw_writing_exception(env, e.what());
} catch (...) {
throw_writing_exception(env, "unknown error");
}
return nullptr;
}
} // namespace
extern "C" JNIEXPORT jobject JNICALL Java_zxingcpp_BarcodeWriter_nativeWriteBitmap(JNIEnv* env, jobject, jbyteArray contents,
jboolean text, jint format, jstring options, jint scale, jint rotate, jboolean add_hrt, jboolean add_quiet_zones)
{
return guarded(env, [&]() -> jobject {
Barcode const barcode = create(env, contents, text, format, options);
return bitmap_of(env, WriteBarcodeToImage(barcode, writer_options(scale, rotate, add_hrt, add_quiet_zones)));
});
}
// the UTF-8 of the SVG, which Kotlin decodes (the human readable text may be any Unicode)
extern "C" JNIEXPORT jbyteArray JNICALL Java_zxingcpp_BarcodeWriter_nativeWriteSvg(JNIEnv* env, jobject, jbyteArray contents,
jboolean text, jint format, jstring options, jint scale, jint rotate, jboolean add_hrt, jboolean add_quiet_zones)
{
return guarded(env, [&]() -> jbyteArray {
Barcode const barcode = create(env, contents, text, format, options);
std::string const svg = WriteBarcodeToSVG(barcode, writer_options(scale, rotate, add_hrt, add_quiet_zones));
jbyteArray const bytes = env->NewByteArray(static_cast<jsize>(svg.size()));
check(env);
env->SetByteArrayRegion(bytes, 0, static_cast<jsize>(svg.size()), reinterpret_cast<jbyte const*>(svg.data()));
check(env);
return bytes;
});
}
build.lua 2676 bytes · raw
-- The CMake project of the reader (with `writers`, the writer too): libZXing.a or libZXing.so and include/ZXing/; or,
-- with `jni`, as the library ZXing, libzxingcpp_android.so, the reader of the Android wrapper (with `writers`, the
-- recipe's writer too) from android/CMakeLists.txt of the recipe, which has no install rules: the library is copied
-- into the prefix.
function build(slice, options, deps)
if options.jni then
if slice.linkage ~= "dynamic" then
error("jni builds a shared object, which Java loads: it needs linkage = \"dynamic\"")
end
otok.cmake{
source = slice.recipe .. "/android",
defines = {
ZXING_SOURCE = slice.source,
ZXING_WRITERS = options.writers and "NEW" or "OFF",
-- the reader goes into the JNI library, which leaves a release build without the debug information
-- of the NDK's C++ runtime, as the wrapper's AAR has it
BUILD_SHARED_LIBS = false,
CMAKE_SHARED_LINKER_FLAGS_RELEASE = slice.configuration == "release" and "-s" or "",
},
targets = { "zxingcpp_android" },
install = false,
}
local p = slice.prefix
otok.copy(slice.build .. "/libzxingcpp_android.so", p .. "/lib/libzxingcpp_android.so")
otok.copy(slice.source .. "/wrappers/android/zxingcpp/src/main/java/zxingcpp/BarcodeReader.kt", p .. "/share/BarcodeReader.kt")
local data = { ["BarcodeReader.kt"] = p .. "/share/BarcodeReader.kt" }
if options.writers then
otok.copy(slice.recipe .. "/android/BarcodeWriter.kt", p .. "/share/BarcodeWriter.kt")
data["BarcodeWriter.kt"] = p .. "/share/BarcodeWriter.kt"
end
-- the library ZXing of the product, under the name Java loads; Java calls it: no headers
otok.mkdir(p .. "/include")
return {
libraries = { ZXing = p .. "/lib/libzxingcpp_android.so" },
headers = p .. "/include",
data = data,
}
end
-- the project of core/, as the wrappers build it: the top one adds, with the C API, the C wrapper's test
-- program, for which it fetches stb; the writer's libzint is the submodule zint/ beside core/
otok.cmake{
source = slice.source .. "/core",
defines = { ZXING_READERS = true, ZXING_WRITERS = options.writers and "NEW" or "OFF", ZXING_C_API = true },
}
local p = slice.prefix
local library = p .. (slice.linkage == "static" and "/lib/libZXing.a" or "/lib/libZXing.so")
return { libraries = { ZXing = library }, headers = p .. "/include" }
end
recipe.toml 2680 bytes · raw
# The otok recipe of zxing-cpp (https://github.com/apotocki/otok): the barcode reader library, with `writers`
# also the writer of its new backend, the bundled libzint (the submodule zint/), for Android. With `jni` the library is
# the JNI library of zxing-cpp's Android wrapper instead, for an app that uses zxingcpp.BarcodeReader without the
# wrapper's AAR, and with `writers` zxingcpp.BarcodeWriter of this recipe (android/) too.
[recipe]
api = 1
library = "zxing-cpp"
name = "zxing-cpp"
version = "3.1.1"
revision = 1
# the reader carries libzueci (core/src/libzueci), the writer libzint (zint/backend), both BSD-3-Clause; the source has
# neither text: zint/LICENSE only says which part of zint is BSD, the text is in the headers of its files. The code of
# android/ and wrappers/ is this recipe's own, CC0-1.0.
license = "Apache-2.0 AND BSD-3-Clause AND CC0-1.0"
license-files = ["LICENSE", { recipe = "LICENSE-libzueci" }, "zint/LICENSE", { recipe = "LICENSE-zint" }]
homepage = "https://github.com/zxing-cpp/zxing-cpp"
description = "zxing-cpp, a barcode reader and writer for linear and matrix formats: QR Code, Micro QR, rMQR, Aztec, DataMatrix, PDF417, MaxiCode, EAN/UPC, Code 39/93/128, ITF, Codabar, DataBar and others."
keywords = ["barcode", "qrcode", "scanner", "zxing"]
linkage = ["static", "dynamic"]
cxx-standard = ">= 20"
[source]
git = "https://github.com/zxing-cpp/zxing-cpp"
commit = "287c85df6f961c8efbfb5ffd736cd9457b8b890e"
tag = "v3.1.1"
# zint/, the writer's backend
submodules = true
[platforms]
supported = ["android"]
build-on = ["*/ndk"]
[options.jni]
type = "bool"
default = false
doc = "Android: libzxingcpp_android.so, the JNI library of zxing-cpp's Android wrapper (wrappers/android), with the reader inside, as the library ZXing of the product ZXing, without headers; data/ of the output has the wrapper's BarcodeReader.kt, the class of zxingcpp.BarcodeReader that loads it, for the app's sources (with the ProGuard rule -keep class zxingcpp.** { *; }: the library makes the class's results through JNI). With writers, also the recipe's writer (android/ZXingCppWriter.cpp) and BarcodeWriter.kt, zxingcpp.BarcodeWriter. Needs linkage = \"dynamic\"."
[options.writers]
type = "bool"
default = false
doc = "The writer of the new backend (ZXING_WRITERS = NEW: CreateBarcode.h, WriteBarcode.h), built from the bundled libzint into the ZXing library; with jni into libzxingcpp_android.so, with zxingcpp.BarcodeWriter."
[toolchains]
cmake = { version = ">= 3.16", default-recipe = "apotocki/cmake" }
ninja = { version = ">= 1.10", default-recipe = "apotocki/ninja" }
[[product]]
name = "ZXing"
libraries = ["ZXing"]
link = { runtime = "c++" }