How,Make,Java,Barcode,Reader,w computer How to Make Java Barcode Reader with Dynamsoft Barcode SDK


----------------------------------------------------------Permission is granted for the below article to forward,reprint, distribute, use for ezine, newsletter, website,offer as free bonus or part of a product for sale as longas no changes a Gone are those times when the companies and the organisations didn't need a hi-tech system to handle them. Owing to the considerable increase in the business sector and thus, an enormous increase in the complexity of the organisational struc


Last week, Dynamsoft released Barcode Reader (DBR) SDK v2.0, which is available for Windows and Mac. The Windows installer contains Barcode libraries for ActiveX, C/C++, and .NET. If you are a Java developer, you have to use JNI to link native C/C++ libraries. In this tutorial, I’ll demonstrate how to invoke the native methods of Dynamsoft Barcode SDK via JNI to create a Java Barcode Reader.JNI for Linking Dynamsoft Barcode Reader DLLDownload and install Dynamsoft Barcode Reader.Create a new Java project in Eclipse. We need a Class for calling native methods.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879package com.dynamsoft.barcode; import java.util.Properties; public class JBarcode {     static {         try {            // get arch            Properties props = System.getProperties();            String bits = String.valueOf(props.get("sun.arch.data.model"));            if (bits.equals("32")) {                bits = "86";            }             String jniLib = "DynamsoftBarcodeJNIx" + bits;             // load dll            System.loadLibrary(jniLib);        } catch (Exception e) {            System.err.println("load jni error!");        } }     public native static int DBR_InitLicense(        String pLicense //License Key    );     public native static int DBR_DecodeFile(        String pFileName,         int option_iMaxBarcodesNumPerPage,        long option_lBarcodeFormat,         tagBarcodeResultArray ppResults //Barcode Results    );      public native static int DBR_DecodeFileRect(        String pFileName,         int option_iMaxBarcodesNumPerPage,        long option_lBarcodeFormat,         int iRectLeft,          //Rectangle Left        int iRectTop,           //Rectangle Top        int iRectWidth,         //Rectangle        int iRectHeight,        //Rectangle        tagBarcodeResultArray ppResults // Barcode Results    );     public native static int DBR_DecodeBuffer(        byte[] pDIBBuffer,  //Buffer        int iDIBSize,         int option_iMaxBarcodesNumPerPage,        long option_lBarcodeFormat,         tagBarcodeResultArray ppResults //Barcode Results    );     public native static int DBR_DecodeBufferRect(        byte[] pDIBBuffer,  //Buffer        int iDIBSize,           int option_iMaxBarcodesNumPerPage,        long option_lBarcodeFormat,         int iRectLeft,          //Rectangle Left        int iRectTop,           //Rectangle Top        int iRectWidth,         //Rectangle        int iRectHeight,        //Rectangle        tagBarcodeResultArray ppResults //Barcode Results    );     public native String GetErrorString(int iErrorCode); }Create a new DLL project in Visual Studio, and then add Include directories of JDK and DBR.Add Library directories of DBR.Declare JNI methods in a header file.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758/* DO NOT EDIT THIS FILE - it is machine generated */#include /* Header for class com_dynamsoft_barcode_JBarcode */ #ifndef _Included_com_dynamsoft_barcode_JBarcode#define _Included_com_dynamsoft_barcode_JBarcode extern "C" {/* * Class:     com_dynamsoft_barcode_JBarcode * Method:    DBR_InitLicense * Signature: (Ljava/lang/String;)I */JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1InitLicense  (JNIEnv *, jclass, jstring); /* * Class:     com_dynamsoft_barcode_JBarcode * Method:    DBR_DecodeFile * Signature: (Ljava/lang/String;IJLcom/dynamsoft/barcode/tagBarcodeResultArray;)I */JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1DecodeFile  (JNIEnv *, jclass, jstring, jint, jlong, jobject); /* * Class:     com_dynamsoft_barcode_JBarcode * Method:    DBR_DecodeFileRect * Signature: (Ljava/lang/String;IJIIIILcom/dynamsoft/barcode/tagBarcodeResultArray;)I */JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1DecodeFileRect  (JNIEnv *, jclass, jstring, jint, jlong, jint, jint, jint, jint, jobject); /* * Class:     com_dynamsoft_barcode_JBarcode * Method:    DBR_DecodeBuffer * Signature: ([BIIJLcom/dynamsoft/barcode/tagBarcodeResultArray;)I */JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1DecodeBuffer  (JNIEnv *, jclass, jbyteArray, jint, jint, jlong, jobject); /* * Class:     com_dynamsoft_barcode_JBarcode * Method:    DBR_DecodeBufferRect * Signature: ([BIIJIIIILcom/dynamsoft/barcode/tagBarcodeResultArray;)I */JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1DecodeBufferRect  (JNIEnv *, jclass, jbyteArray, jint, jint, jlong, jint, jint, jint, jint, jobject); /* * Class:     com_dynamsoft_barcode_JBarcode * Method:    GetErrorString * Signature: (I)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_dynamsoft_barcode_JBarcode_GetErrorString  (JNIEnv *, jclass, jint); }#endifAdd implementations in relevant CPP file. Make sure your Visual Studio can find the included header files and libraries.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261#include "com_dynamsoft_barcode_JBarcode.h" // BarcodeReaderDemo.cpp : Defines the entry point for the console application. #include #include "../../../Include/If_DBR.h"#include "../../../Include/BarcodeFormat.h"#include "../../../Include/BarcodeStructs.h"#include "../../../Include/ErrorCode.h" #ifdef _WIN64#pragma comment(lib, "DBRx64.lib")#else#pragma comment(lib, "DBRx86.lib")#endif bool isGetClassBarcodeResult = false;jclass    br_m_cls   = NULL;jmethodID br_m_mid   = NULL;jfieldID  br_m_Format = NULL;jfieldID  br_m_BarcodeData = NULL;jfieldID  br_m_BarcodeDataLength = NULL;jfieldID  br_m_Left = NULL;jfieldID  br_m_Top = NULL;jfieldID  br_m_Width = NULL;jfieldID  br_m_Height = NULL;jfieldID  br_m_X1 = NULL;jfieldID  br_m_Y1 = NULL;jfieldID  br_m_X2 = NULL;jfieldID  br_m_Y2 = NULL;jfieldID  br_m_X3 = NULL;jfieldID  br_m_Y3 = NULL;jfieldID  br_m_X4 = NULL;jfieldID  br_m_Y4 = NULL;jfieldID  br_m_PageNum = NULL; bool isGetClassBarcodeArrayResult = false;jclass    brAry_cls   = NULL;jmethodID brAry_mid   = NULL;jfieldID  brAry_field_count = NULL;jfieldID  brAry_field_brResult = NULL; void loadJavaClassInfo(JNIEnv *env){    if(!isGetClassBarcodeResult){        br_m_cls   = env->FindClass("com/dynamsoft/barcode/tagBarcodeResult");        br_m_mid   = env->GetMethodID(br_m_cls,"","()V");         br_m_Format = env->GetFieldID(br_m_cls,"lFormat","J");        br_m_BarcodeData = env->GetFieldID(br_m_cls,"pBarcodeData","[B");        br_m_BarcodeDataLength = env->GetFieldID(br_m_cls,"iBarcodeDataLength","I");        br_m_Left = env->GetFieldID(br_m_cls,"iLeft","I");        br_m_Top = env->GetFieldID(br_m_cls,"iTop","I");        br_m_Width = env->GetFieldID(br_m_cls,"iWidth","I");        br_m_Height = env->GetFieldID(br_m_cls,"iHeight","I");        br_m_X1 = env->GetFieldID(br_m_cls,"iX1","I");        br_m_Y1 = env->GetFieldID(br_m_cls,"iY1","I");        br_m_X2 = env->GetFieldID(br_m_cls,"iX2","I");        br_m_Y2 = env->GetFieldID(br_m_cls,"iY2","I");        br_m_X3 = env->GetFieldID(br_m_cls,"iX3","I");        br_m_Y3 = env->GetFieldID(br_m_cls,"iY3","I");        br_m_X4 = env->GetFieldID(br_m_cls,"iX4","I");        br_m_Y4 = env->GetFieldID(br_m_cls,"iY4","I");        br_m_PageNum = env->GetFieldID(br_m_cls,"iPageNum","I");        isGetClassBarcodeResult = true;    }     if(!isGetClassBarcodeArrayResult){        brAry_cls   = env->FindClass("com/dynamsoft/barcode/tagBarcodeResultArray");        brAry_mid   = env->GetMethodID(brAry_cls,"","()V");        brAry_field_count = env->GetFieldID(brAry_cls,"iBarcodeCount","I");        brAry_field_brResult = env->GetFieldID(brAry_cls,"ppBarcodes","[Lcom/dynamsoft/barcode/tagBarcodeResult;");        isGetClassBarcodeArrayResult = true;    }} jobject convertResultToJNIObject(JNIEnv *env, pBarcodeResult pBarcode){     loadJavaClassInfo(env);     jobject obj = env->NewObject(br_m_cls, br_m_mid);     jbyteArray rtnbytes = env->NewByteArray((jsize)(pBarcode->iBarcodeDataLength));    env->SetByteArrayRegion(rtnbytes, 0, (jsize)(pBarcode->iBarcodeDataLength), (jbyte*)pBarcode->pBarcodeData);     env->SetLongField(obj, br_m_Format, pBarcode->llFormat);    env->SetObjectField(obj, br_m_BarcodeData, rtnbytes);    env->SetIntField(obj, br_m_BarcodeDataLength, pBarcode->iBarcodeDataLength);    env->SetIntField(obj, br_m_Left, pBarcode->iLeft);    env->SetIntField(obj, br_m_Top, pBarcode->iTop);    env->SetIntField(obj, br_m_Width, pBarcode->iWidth);    env->SetIntField(obj, br_m_Height, pBarcode->iHeight);    env->SetIntField(obj, br_m_X1, pBarcode->iX1);    env->SetIntField(obj, br_m_Y1, pBarcode->iY1);    env->SetIntField(obj, br_m_X2, pBarcode->iX2);    env->SetIntField(obj, br_m_Y2, pBarcode->iY2);    env->SetIntField(obj, br_m_X3, pBarcode->iX3);    env->SetIntField(obj, br_m_Y3, pBarcode->iY3);    env->SetIntField(obj, br_m_X4, pBarcode->iX4);    env->SetIntField(obj, br_m_Y4, pBarcode->iY4);    env->SetIntField(obj, br_m_PageNum, pBarcode->iPageNum);     return obj;} void fillBarcodeResultArray(JNIEnv *env, jobject obj, pBarcodeResultArray pArrayResults){     loadJavaClassInfo(env);     int count = pArrayResults->iBarcodeCount;    pBarcodeResult* ppBarcodes = pArrayResults->ppBarcodes;     jobjectArray swArray = env->NewObjectArray(count, br_m_cls, 0);     for(int i=0; iSetObjectArrayElement(swArray, i, convertResultToJNIObject(env, ppBarcodes[i]));    }     env->SetIntField(obj, brAry_field_count, count);    env->SetObjectField(obj, brAry_field_brResult, swArray); } void SetOptions(pReaderOptions pOption, jint option_iMaxBarcodesNumPerPage, jlong option_llBarcodeFormat){     if(option_llBarcodeFormat > 0)        pOption->llBarcodeFormat = option_llBarcodeFormat;    else        pOption->llBarcodeFormat = OneD;     if(option_iMaxBarcodesNumPerPage > 0)        pOption->iMaxBarcodesNumPerPage = option_iMaxBarcodesNumPerPage;    else        pOption->iMaxBarcodesNumPerPage = MAXINT; } JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1InitLicense(JNIEnv *env, jclass, jstring pString){     const char *nativeString = env->GetStringUTFChars(pString, 0);      //printf("%s", nativeString);    int ret = DBR_InitLicense(nativeString);     //DON'T FORGET THIS LINE!!!     env->ReleaseStringUTFChars(pString, nativeString);      return ret;} JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1DecodeFile  (JNIEnv *env, jclass, jstring strFileName, jint option_iMaxBarcodesNumPerPage, jlong option_llBarcodeFormat, jobject pArrayResults){    const char *pFileName = env->GetStringUTFChars(strFileName, 0);    pBarcodeResultArray pResults = NULL;    ReaderOptions option;    SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat);     int ret = DBR_DecodeFile(        pFileName,        &option,        &pResults    );     if(ret == DBR_OK){        fillBarcodeResultArray(env, pArrayResults, pResults);        DBR_FreeBarcodeResults(&pResults);    }     //DON'T FORGET THIS LINE!!!     env->ReleaseStringUTFChars(strFileName, pFileName);     return ret;} JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1DecodeFileRect  (JNIEnv *env, jclass, jstring strFileName, jint option_iMaxBarcodesNumPerPage, jlong option_llBarcodeFormat, jint iRectLeft, jint iRectTop, jint iRectWidth, jint iRectHeight, jobject pArrayResults){    const char *pFileName = env->GetStringUTFChars(strFileName, 0);    pBarcodeResultArray pResults = NULL;    ReaderOptions option;    SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat);     int ret = DBR_DecodeFileRect(        pFileName,        &option,        iRectLeft,         iRectTop,          iRectWidth,        iRectHeight,        &pResults    );     if(ret == DBR_OK){         fillBarcodeResultArray(env, pArrayResults, pResults);        DBR_FreeBarcodeResults(&pResults);    }     //DON'T FORGET THIS LINE!!!     env->ReleaseStringUTFChars(strFileName, pFileName);     return ret;} JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1DecodeBuffer  (JNIEnv *env, jclass, jbyteArray pDIBBuffer, jint iDIBSize, jint option_iMaxBarcodesNumPerPage, jlong option_llBarcodeFormat, jobject pArrayResults){    pBarcodeResultArray pResults = NULL;    ReaderOptions option;    SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat);     int ret = DBR_DecodeBuffer(        (unsigned char*) pDIBBuffer,               iDIBSize,        &option,           &pResults    );     if(ret == DBR_OK){        fillBarcodeResultArray(env, pArrayResults, pResults);        DBR_FreeBarcodeResults(&pResults);    }     return ret;} JNIEXPORT jint JNICALL Java_com_dynamsoft_barcode_JBarcode_DBR_1DecodeBufferRect  (JNIEnv *env, jclass,  jbyteArray pDIBBuffer, jint iDIBSize, jint option_iMaxBarcodesNumPerPage, jlong option_llBarcodeFormat,  jint iRectLeft, jint iRectTop, jint iRectWidth, jint iRectHeight, jobject pArrayResults){    pBarcodeResultArray pResults = NULL;    ReaderOptions option;    SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat);     int ret = DBR_DecodeBufferRect(        (unsigned char*) pDIBBuffer,               iDIBSize,        &option,        iRectLeft,         iRectTop,          iRectWidth,        iRectHeight,        &pResults    );     if(ret == DBR_OK){        fillBarcodeResultArray(env, pArrayResults, pResults);        DBR_FreeBarcodeResults(&pResults);    }     return ret;} JNIEXPORT jstring JNICALL Java_com_dynamsoft_barcode_JBarcode_GetErrorString  (JNIEnv *env, jclass, jint iErrorCode){    const char *pError = GetErrorString( iErrorCode);    return env->NewStringUTF(pError);    // (*env)->ReleaseStringUTFChars(env, jstr, utf); }Build the project, and copy the generated DLL and DBR DLL to the Java Barcode Reader project.Finally, we can specify the image file path as the argument, and run the Java project.Source Codehttps://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/Java

How,Make,Java,Barcode,Reader,w

computer

Equipment Rental Software – Features And Cost

Equipment rental management software is an essential thing these days for any equipment rental company.A well-developed equipment rental software provides you with a variety of features that can really help you maintain and organise your cus ...

computer

5 Big Reasons Why I Migrated From Angularjs To React

I have 5 main reasons for my angularjs to react migration. No, it's not a comparison on which is better. A comparison between apples and oranges would make no point. React is a library, and angular is a framework. Both can do stuff in their ...

computer

How to troubleshoot McAfee error 2318?

Security software means McAfee! For many computer users, McAfee antivirus is the only choice for security software as it provides all the features and tools which are necessary for device and data protection. This robust antivirus merely sho ...

computer

Manage Multiple Counter With AlignBooks Point of Sale

Fulfilling your businesss needs which can grow your firm is our aim. AlignBooks is better known for providing a strong pillar to newly started or midway businesss. Those companies who dont want to fall back with irregularity manage the inven ...

computer

How to Autoplay Embedded YouTube Videos

Source: How to Autoplay Embedded YouTube VideosEmbedding a video or audio enables the users to share their videos with any of their preferred sites or any social networking platforms. They can do so by copying the embedded link of the parti ...

computer

3 Major Mistakes to Avoid in Retail Business

Truth be told, nearly half of the retail businesses survive longer than four years and which can be something to ponder for a newbie before stepping into the industry. However, this being said, it is also true that you can excel in the indus ...

computer

Start Your Own Computer Repair Business

1. Know your street value. In the early 90's, running a PC repair business centered around selling parts and products, with service on the side. Today, it's about selling hours. If you run a business, you need to consider the X3 rule. That m ...

computer

How Establishments Show Up in Restaurant Searches

The revolutionary rise of technology has made things easy-peasy for consumers in the restaurant industry. Unlike the old days, the availability of innumerable platforms has made it possible for diners to choose from various searching options ...

computer

GuildWars 2 :

The last expansion pack for Guild Wars 2 was Path of Fire, which was released in 2017 and brings you a new enemy-Balthazar, the evil god of war. Although this doesn't sound like another expansion pack currently in production, some fans ma ...

computer

Customer Support at the time of COVID-19 Pandemic

COVID-19 is the worst crisis of our time as we observe social distancing protocols being imposed all around the world. While these measures are a step in effectively managing the COVID-19 pandemic, Hospitality and Retail businesses are confr ...

computer

How to Choose a Contract Management Solution (CLM)?

Contract life cycle management (CLM) systems can simplify and automate contract creation, negotiation, execution and storage. They are an intelligent alternative to the tedious hand tools formerly used for these tasks, which lacked visibili ...

computer

Contacting Google Live Person to Resolve Your Issues

Users are fond of all the Google supported products and look forward to the best services. Also, Google as a whole has never disappointed its users and helped them at every point with its commendable services. Also, being a customer-oriente ...

computer

how to uninstall discord

How to Uninstall Discord in Windows 10? has supported open source technologies, our tool is secure and safe to use. To uninstall a discord from your windows, you'll use this method which is given below.USING THIRD PARTY TOOLS1. Firstly, you ...