Recent

Author Topic: LAMW - barcodegen QrCode creation addition  (Read 1835 times)

Mongkey

  • Sr. Member
  • ****
  • Posts: 435
LAMW - barcodegen QrCode creation addition
« on: September 15, 2023, 08:23:55 am »
source : https://www.callicoder.com/generate-qr-code-in-java-using-zxing/

Code: Java  [Select][+][-]
  1. package org.lamw.appbarcodegendemo1;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.Color;
  6. import android.net.Uri;
  7.  
  8. import com.google.zxing.BarcodeFormat;
  9. import com.google.zxing.MultiFormatWriter;
  10. import com.google.zxing.WriterException;
  11. import com.google.zxing.common.BitMatrix;
  12. import com.google.zxing.qrcode.QRCodeWriter;
  13.  
  14. import java.io.IOException;
  15. import java.text.CharacterIterator;
  16. import java.text.StringCharacterIterator;
  17. import java.util.Arrays;
  18.  
  19. /*Draft java code by "Lazarus Android Module Wizard" [7/25/2020 0:50:10]*/
  20. /*https://github.com/jmpessoa/lazandroidmodulewizard*/
  21. /*jControl LAMW template*/
  22.  
  23. //ref. https://stackoverflow.com/questions/22371626/android-generate-qr-code-and-barcode-using-zxing
  24. public class jBarcodeGen /*extends ...*/ {
  25.  
  26.     private long pascalObj = 0;        //Pascal Object
  27.     private Controls controls  = null; //Java/Pascal [events] Interface ...
  28.     private Context  context   = null;
  29.  
  30.     //GUIDELINE: please, preferentially, init all yours params names with "_", ex: int _flag, String _hello ...
  31.  
  32.     public jBarcodeGen(Controls _ctrls, long _Self) { //Add more others news "_xxx" params if needed!
  33.         //super(_ctrls.activity);
  34.         context   = _ctrls.activity;
  35.         pascalObj = _Self;
  36.         controls  = _ctrls;
  37.     }
  38.  
  39.     public void jFree() {
  40.         //free local objects...
  41.     }
  42.  
  43.     //write others [public] methods code here......
  44.     //GUIDELINE: please, preferentially, init all yours params names with "_", ex: int _flag, String _hello ...
  45.  
  46.     /*
  47.  
  48.     CODABAR,
  49.     CODE_39,
  50.     CODE_93,
  51.     CODE_128,
  52.     EAN_8, EAN_13,
  53.     ITF,
  54.     RSS_14,
  55.     RSS_EXPANDED,
  56.     UPC_A,
  57.     UPC_E,
  58.     UPC_EAN_EXTENSION;
  59.  
  60.     AZTEC,
  61.     MAXICODE,
  62.     PDF_417,
  63.     DATA_MATRIX,
  64.     QR_CODE,
  65.  
  66.      */
  67.     //note: QRCode, Data Matrix, Aztec, PDF 417 and MaxiCode all are 2D barcodes
  68.     //UCC-128, EAN-128 or SSCC-18       shipping cartons
  69.     //https://www.morovia.com/kb/Identify-Barcode-Format-10623.html
  70.  
  71.     //https://www.scandit.com/blog/types-of-barcodes-choosing-the-right-barcode-type-ean-upc-code128-itf-14-or-code39/
  72.  
  73.     public Bitmap QRCode(String _data, int _width, int _height)
  74.             throws WriterException, IOException {
  75. //        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
  76. //        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage
  77.  
  78.         QRCodeWriter qrCodeWriter = new QRCodeWriter();
  79.  
  80.         BitMatrix bitMatrix = qrCodeWriter.encode(_data,BarcodeFormat.QR_CODE, _width, _height);
  81.         int width = bitMatrix.getWidth();
  82.         int height = bitMatrix.getHeight();
  83.         int[] pixels = new int[width * height];
  84.         for (int y = 0; y < height; y++) {
  85.             int offset = y * width;
  86.             for (int x = 0; x < width; x++) {
  87.                 pixels[offset + x] = bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE;
  88.             }
  89.         }
  90.  
  91.         Bitmap imageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  92.         imageBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  93.         return imageBitmap;
  94.     }
  95.  
  96.     public Bitmap Get128Bar(String _data, int _width, int _height) throws WriterException {
  97.         MultiFormatWriter writer = new MultiFormatWriter();
  98.         String finalData = Uri.encode(_data);
  99.  
  100.         // Use 1 as the height of the matrix as this is a 1D Barcode.
  101.         BitMatrix bm = writer.encode(finalData, BarcodeFormat.CODE_128, _width, 1);
  102.         int bmWidth = bm.getWidth();
  103.  
  104.         Bitmap imageBitmap = Bitmap.createBitmap(bmWidth, _height, Bitmap.Config.ARGB_8888);
  105.  
  106.         for (int i = 0; i < bmWidth; i++) {
  107.             // Paint columns of width 1
  108.             int[] column = new int[_height];
  109.             Arrays.fill(column, bm.get(i, 0) ? Color.BLACK : Color.WHITE);
  110.             imageBitmap.setPixels(column, 0, 1, i, 0, 1, _height);
  111.         }
  112.         return imageBitmap;
  113.     }
  114.  
  115.     /*
  116.     CODE_128,
  117.     EAN_8,
  118.     EAN_13,
  119.     ITF,
  120.     UPC_A,
  121.     UPC_E,
  122.     CODE_39,
  123.  
  124.     AZTEC,
  125.     MAXICODE,
  126.     PDF_417,
  127.     DATA_MATRIX,
  128.     QR_CODE,
  129.      */
  130.  
  131.     //ref. https://stackoverflow.com/questions/10143547/how-do-i-validate-a-upc-or-ean-code
  132.     //UPC-A encodes 11 digits
  133.     //http://www.barcodeisland.com/upca.phtml
  134.     /*
  135.     UPC-A has the following structure:  http://www.barcodelib.com/java_barcode/barcode_symbologies/upca.html
  136.     1 digit for Number System (0: regular UPC codes, 1: reserved, 2: random weight items marked at the store, 3: National Drug Code and National Health Related Items code, 4: no format restrictions, for in-store use on non-food items, 5: for use on coupons, 6: reserved, 7: regular UPC codes, 8: reserved, 9: reserved).
  137.     5 digits for Manufacturer (Company) Code or prefix. This number is assigned by the Uniform Code Council (UCC).
  138.     5 digits for Product Code which is assigned by the manufacturer.
  139.     1 digit for checksum.
  140.      */
  141.     //UPC-E 0463783-7 //https://www.barcodesinc.com/articles/upce.htm
  142.     //http://www.keepautomation.com/upce/
  143.     public String GetUPCAChecksum(String _data11digits) { //01234567890-5 //04210000526-4   //12345678901-2
  144.         long num = Long.valueOf(_data11digits);
  145.         int odd_sum = 0, even_sum = 0;
  146.         boolean odd = true;
  147.         while (num != 0) {
  148.             if (odd) {
  149.                 odd_sum += num%10;
  150.                 odd = false;
  151.             } else {
  152.                 even_sum += num%10;
  153.                 odd = true;
  154.             }
  155.             num = (num - num%10)/10; // reduce number
  156.         }
  157.         int calcCheckDigit = (10 - ((even_sum  + 3 * odd_sum) % 10)) % 10;
  158.         return String.valueOf(calcCheckDigit);
  159.     }
  160.  
  161.     /*
  162.     UPC-E barcode image has the following structure: http://www.barcodelib.com/java_barcode/barcode_symbologies/upce.html
  163.     1 digit for Number System (0), set by library automatically.
  164.     6 digits for UPCE data set through Data property
  165.     1 digit for checksum, calculated automatically by barcode library.
  166.      */
  167.     //TODO
  168.     //public String GetUPCEChecksum(String _data6digits) {  //123450 or[7] 0123450 ??
  169.     public String GetUPCEChecksum(String _data7digits) { //01234567890-5 //04210000526-4   //12345678901-2
  170.         long num = Long.valueOf(_data7digits);
  171.         int odd_sum = 0, even_sum = 0;
  172.         boolean odd = true;
  173.         while (num != 0) {
  174.             if (odd) {
  175.                 odd_sum += num%10;
  176.                 odd = false;
  177.             } else {
  178.                 even_sum += num%10;
  179.                 odd = true;
  180.             }
  181.             num = (num - num%10)/10; // reduce number
  182.         }
  183.         int calcCheckDigit = (10 - ((even_sum  + 3 * odd_sum) % 10)) % 10;
  184.         return String.valueOf(calcCheckDigit);
  185.     }
  186.     //}
  187.  
  188.     // ref. http://www.acordex.com/consulting/BarCode39.html
  189.     public String GetCode39Checksum(String _dataCode) throws Exception {
  190.         String charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
  191.         int total = 0;
  192.         CharacterIterator it = new StringCharacterIterator(_dataCode);
  193.         for (char ch = it.current(); ch != CharacterIterator.DONE; ch = it.next()) {
  194.             int charValue = charSet.indexOf(ch);
  195.             if (charValue == -1) {
  196.                 // Invalid character.
  197.                 throw new Exception("Input String '" +_dataCode+ "' contains characters that are invalid in a Code39 barcode.");
  198.             }
  199.             total += charValue;
  200.         }
  201.         int checksum = total % 43;
  202.         return Character.toString(charSet.charAt(checksum));
  203.     }
  204.  
  205.     //ref. https://stackoverflow.com/questions/1136642/ean-8-how-to-calculate-checksum-digit
  206.     public String GetEAN13Checksum(String _data12digits) {
  207.         int first = 0;
  208.         int second = 0;
  209.         int roundedNum = 0;
  210.         int total = 0;
  211.  
  212.         if( _data12digits.length() != 12) return "?";
  213.  
  214.         for (int counter = 0; counter < _data12digits.length() - 1; counter++) {
  215.             first = (first + Integer.valueOf(_data12digits.substring(counter, counter + 1)));
  216.             counter++;
  217.             second = (second + Integer.valueOf(_data12digits.substring(counter, counter + 1)));
  218.         }
  219.  
  220.         second = second * 3;
  221.         total = second + first;
  222.         roundedNum = Math.round((total + 9) / 10 * 10);
  223.  
  224.         return String.valueOf(roundedNum - total);
  225.     }
  226.  
  227.     //ref. https://stackoverflow.com/questions/1136642/ean-8-how-to-calculate-checksum-digit
  228.     public String GetEAN8Checksum(String _data7digits) {
  229.  
  230.         String code= _data7digits; //"55123457";
  231.  
  232.         if(code.length() != 7) return "?";
  233.  
  234.         char[] acode = code.toCharArray();
  235.         int sum1 = acode[1] + acode[3] + acode[5];
  236.         int sum2 = 3 * (acode[0] + acode[2] + acode[4] + acode[6]);
  237.         int checksum_value = sum1 + sum2;
  238.  
  239.         int checksum_digit = 10 - (checksum_value % 10);
  240.         if (checksum_digit == 10)
  241.             checksum_digit = 0;
  242.  
  243.         return String.valueOf(checksum_digit);
  244.     }
  245.  
  246.     public Bitmap GetBar1D(int _format, String _data, int _width, int _height) throws WriterException {
  247.         MultiFormatWriter writer = new MultiFormatWriter();
  248.         String finalData = Uri.encode(_data);
  249.         BitMatrix bm;
  250.         switch (_format) {
  251.             case 0: bm = writer.encode(finalData, BarcodeFormat.CODE_128, _width, 1);
  252.             case 1: bm = writer.encode(finalData, BarcodeFormat.EAN_8, _width, 1); break;
  253.             case 2: bm = writer.encode(finalData, BarcodeFormat.EAN_13, _width, 1); break;
  254.             case 3: bm = writer.encode(finalData, BarcodeFormat.ITF, _width, 1); break;
  255.             case 4: bm = writer.encode(finalData, BarcodeFormat.UPC_A, _width, 1); break;
  256.             case 5: bm = writer.encode(finalData, BarcodeFormat.UPC_E, _width, 1); break;
  257.             case 6: bm = writer.encode(finalData, BarcodeFormat.CODE_39, _width, 1); break;
  258.             default:
  259.                 bm = writer.encode(finalData, BarcodeFormat.CODE_128, _width, 1);
  260.         }
  261.         // Use 1 as the height of the matrix as this is a 1D Barcode.
  262.         int bmWidth = bm.getWidth();
  263.         Bitmap imageBitmap = Bitmap.createBitmap(bmWidth, _height, Bitmap.Config.ARGB_8888);
  264.  
  265.         for (int i = 0; i < bmWidth; i++) {
  266.             // Paint columns of width 1
  267.             int[] column = new int[_height];
  268.             Arrays.fill(column, bm.get(i, 0) ? Color.BLACK : Color.WHITE);
  269.             imageBitmap.setPixels(column, 0, 1, i, 0, 1, _height);
  270.         }
  271.         return imageBitmap;
  272.     }
  273.  
  274.     //TODO
  275.     public Bitmap GetBar2D(String _data, int _width, int _height, int _format) throws WriterException {
  276.         MultiFormatWriter writer = new MultiFormatWriter();
  277.         String finalData = Uri.encode(_data);
  278.         BitMatrix bm;
  279.         switch (_format) {
  280.             case 0: bm = writer.encode(finalData, BarcodeFormat.QR_CODE, _width, 1);
  281.             case 1: bm = writer.encode(finalData, BarcodeFormat.DATA_MATRIX, _width, 1); break;
  282.             case 2: bm = writer.encode(finalData, BarcodeFormat.AZTEC, _width, 1); break;
  283.             default:
  284.                 bm = writer.encode(finalData, BarcodeFormat.QR_CODE, _width, 1);
  285.         }
  286.         // Use 1 as the height of the matrix as this is a 1D Barcode.
  287.         int bmWidth = bm.getWidth();
  288.         Bitmap imageBitmap = Bitmap.createBitmap(bmWidth, _height, Bitmap.Config.ARGB_8888);
  289.  
  290.         for (int i = 0; i < bmWidth; i++) {
  291.             // Paint columns of width 1
  292.             //int[] column = new int[_height];
  293.             //Arrays.fill(column, bm.get(i, 0) ? Color.BLACK : Color.WHITE);
  294.             //imageBitmap.setPixels(column, 0, 1, i, 0, 1, _height);
  295.         }
  296.         return imageBitmap;
  297.     }
  298.  
  299. }
  300.  
  301.  
« Last Edit: September 15, 2023, 02:21:08 pm by Mongkey »

Mongkey

  • Sr. Member
  • ****
  • Posts: 435
Re: LAMW - barcodegen QrCode creation addition
« Reply #1 on: September 15, 2023, 08:43:16 am »
Code: Pascal  [Select][+][-]
  1. unit barcodegen;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, And_jni, {And_jni_Bridge,} AndroidWidget;
  9.  
  10. type
  11.  
  12. {Draft Component code by "LAMW: Lazarus Android Module Wizard" [9/15/2023 13:29:23]}
  13. {https://github.com/jmpessoa/lazandroidmodulewizard}
  14.  
  15. {jControl template}
  16.  TBarFormat1D = (fmCODE_128, fmEAN_8, fmEAN_13, fmITF, fmUPC_A, fmUPC_E, fmCODE_39);
  17.  
  18. jBarcodeGen = class(jControl)
  19.  private
  20.  
  21.  public
  22.     constructor Create(AOwner: TComponent); override;
  23.     destructor  Destroy; override;
  24.     procedure Init; override;
  25.     function jCreate(): jObject;
  26.     procedure jFree();
  27.     function QRCode(text: string; width: integer; height: integer): jObject;
  28.     function Get128Bar(_data: string; _width: integer; _height: integer): jObject;
  29.     function GetUPCAChecksum(_data11digits: string): string;
  30.     function GetUPCEChecksum(_data7digits: string): string;
  31.     function GetCode39Checksum(_dataCode: string): string;
  32.     function GetEAN13Checksum(_data12digits: string): string;
  33.     function GetEAN8Checksum(_data7digits: string): string;
  34.     function GetBar1D(_format: integer; _data: string; _width: integer; _height: integer): jObject;
  35.     function GetBar2D(_data: string; _width: integer; _height: integer; _format: integer): jObject;
  36.  
  37.  published
  38.  
  39. end;
  40.  
  41. function jBarcodeGen_jCreate(env: PJNIEnv;_Self: int64; this: jObject): jObject;
  42. procedure jBarcodeGen_jFree(env: PJNIEnv; _jbarcodegen: JObject);
  43. function jBarcodeGen_QRCode(env: PJNIEnv; _jbarcodegen: JObject; text: string; width: integer; height: integer): jObject;
  44. function jBarcodeGen_Get128Bar(env: PJNIEnv; _jbarcodegen: JObject; _data: string; _width: integer; _height: integer): jObject;
  45. function jBarcodeGen_GetUPCAChecksum(env: PJNIEnv; _jbarcodegen: JObject; _data11digits: string): string;
  46. function jBarcodeGen_GetUPCEChecksum(env: PJNIEnv; _jbarcodegen: JObject; _data7digits: string): string;
  47. function jBarcodeGen_GetCode39Checksum(env: PJNIEnv; _jbarcodegen: JObject; _dataCode: string): string;
  48. function jBarcodeGen_GetEAN13Checksum(env: PJNIEnv; _jbarcodegen: JObject; _data12digits: string): string;
  49. function jBarcodeGen_GetEAN8Checksum(env: PJNIEnv; _jbarcodegen: JObject; _data7digits: string): string;
  50. function jBarcodeGen_GetBar1D(env: PJNIEnv; _jbarcodegen: JObject; _format: integer; _data: string; _width: integer; _height: integer): jObject;
  51. function jBarcodeGen_GetBar2D(env: PJNIEnv; _jbarcodegen: JObject; _data: string; _width: integer; _height: integer; _format: integer): jObject;
  52.  
  53.  
  54. implementation
  55.  
  56. {---------  jBarcodeGen  --------------}
  57.  
  58. constructor jBarcodeGen.Create(AOwner: TComponent);
  59. begin
  60.   inherited Create(AOwner);
  61. //your code here....
  62. end;
  63.  
  64. destructor jBarcodeGen.Destroy;
  65. begin
  66.   if not (csDesigning in ComponentState) then
  67.   begin
  68.      if FjObject <> nil then
  69.      begin
  70.        jFree();
  71.        FjObject:= nil;
  72.      end;
  73.   end;
  74.   //you others free code here...'
  75.   inherited Destroy;
  76. end;
  77.  
  78. procedure jBarcodeGen.Init;
  79. begin
  80.  
  81.   if FInitialized  then Exit;
  82.   inherited Init; //set default ViewParent/FjPRLayout as jForm.View!
  83.   //your code here: set/initialize create params....
  84.   FjObject := jCreate(); //jSelf !
  85.  
  86.   if FjObject = nil then exit;
  87.  
  88.   FInitialized:= True;
  89. end;
  90.  
  91.  
  92. function jBarcodeGen.jCreate(): jObject;
  93. begin
  94.    Result:= jBarcodeGen_jCreate(gApp.jni.jEnv, int64(Self), gApp.jni.jThis);
  95. end;
  96.  
  97. procedure jBarcodeGen.jFree();
  98. begin
  99.   //in designing component state: set value here...
  100.   if FInitialized then
  101.      jBarcodeGen_jFree(gApp.jni.jEnv, FjObject);
  102. end;
  103.  
  104. function jBarcodeGen.QRCode(text: string; width: integer; height: integer): jObject;
  105. begin
  106.   //in designing component state: result value here...
  107.   if FInitialized then
  108.    Result:= jBarcodeGen_QRCode(gApp.jni.jEnv, FjObject, text ,width ,height);
  109. end;
  110.  
  111. function jBarcodeGen.Get128Bar(_data: string; _width: integer; _height: integer): jObject;
  112. begin
  113.   //in designing component state: result value here...
  114.   if FInitialized then
  115.    Result:= jBarcodeGen_Get128Bar(gApp.jni.jEnv, FjObject, _data ,_width ,_height);
  116. end;
  117.  
  118. function jBarcodeGen.GetUPCAChecksum(_data11digits: string): string;
  119. begin
  120.   //in designing component state: result value here...
  121.   if FInitialized then
  122.    Result:= jBarcodeGen_GetUPCAChecksum(gApp.jni.jEnv, FjObject, _data11digits);
  123. end;
  124.  
  125. function jBarcodeGen.GetUPCEChecksum(_data7digits: string): string;
  126. begin
  127.   //in designing component state: result value here...
  128.   if FInitialized then
  129.    Result:= jBarcodeGen_GetUPCEChecksum(gApp.jni.jEnv, FjObject, _data7digits);
  130. end;
  131.  
  132. function jBarcodeGen.GetCode39Checksum(_dataCode: string): string;
  133. begin
  134.   //in designing component state: result value here...
  135.   if FInitialized then
  136.    Result:= jBarcodeGen_GetCode39Checksum(gApp.jni.jEnv, FjObject, _dataCode);
  137. end;
  138.  
  139. function jBarcodeGen.GetEAN13Checksum(_data12digits: string): string;
  140. begin
  141.   //in designing component state: result value here...
  142.   if FInitialized then
  143.    Result:= jBarcodeGen_GetEAN13Checksum(gApp.jni.jEnv, FjObject, _data12digits);
  144. end;
  145.  
  146. function jBarcodeGen.GetEAN8Checksum(_data7digits: string): string;
  147. begin
  148.   //in designing component state: result value here...
  149.   if FInitialized then
  150.    Result:= jBarcodeGen_GetEAN8Checksum(gApp.jni.jEnv, FjObject, _data7digits);
  151. end;
  152.  
  153. function jBarcodeGen.GetBar1D(_format: integer; _data: string; _width: integer; _height: integer): jObject;
  154. begin
  155.   //in designing component state: result value here...
  156.   if FInitialized then
  157.    Result:= jBarcodeGen_GetBar1D(gApp.jni.jEnv, FjObject, _format ,_data ,_width ,_height);
  158. end;
  159.  
  160. function jBarcodeGen.GetBar2D(_data: string; _width: integer; _height: integer; _format: integer): jObject;
  161. begin
  162.   //in designing component state: result value here...
  163.   if FInitialized then
  164.    Result:= jBarcodeGen_GetBar2D(gApp.jni.jEnv, FjObject, _data ,_width ,_height ,_format);
  165. end;
  166.  
  167. {-------- jBarcodeGen_JNI_Bridge ----------}
  168.  
  169. function jBarcodeGen_jCreate(env: PJNIEnv;_Self: int64; this: jObject): jObject;
  170. var
  171.   jParams: array[0..0] of jValue;
  172.   jMethod: jMethodID=nil;
  173.   jCls: jClass=nil;
  174. label
  175.   _exceptionOcurred;
  176. begin
  177.  
  178.   Result := nil;
  179.  
  180.   if (env = nil) or (this = nil) then exit;
  181.   jCls:= Get_gjClass(env);
  182.   if jCls = nil then goto _exceptionOcurred;
  183.   jMethod:= env^.GetMethodID(env, jCls, 'jBarcodeGen_jCreate', '(J)Ljava/lang/Object;');
  184.   if jMethod = nil then goto _exceptionOcurred;
  185.  
  186.   jParams[0].j:= _Self;
  187.  
  188.   Result:= env^.CallObjectMethodA(env, this, jMethod, @jParams);
  189.  
  190.   Result:= env^.NewGlobalRef(env, Result);
  191.  
  192.   _exceptionOcurred: if jni_ExceptionOccurred(env) then result := nil;
  193. end;
  194.  
  195.  
  196. procedure jBarcodeGen_jFree(env: PJNIEnv; _jbarcodegen: JObject);
  197. var
  198.   jMethod: jMethodID=nil;
  199.   jCls: jClass=nil;
  200. label
  201.   _exceptionOcurred;
  202. begin
  203.  
  204.   if (env = nil) or (_jbarcodegen = nil) then exit;
  205.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  206.   if jCls = nil then goto _exceptionOcurred;
  207.   jMethod:= env^.GetMethodID(env, jCls, 'jFree', '()V');
  208.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  209.  
  210.   env^.CallVoidMethod(env, _jbarcodegen, jMethod);
  211.  
  212.   env^.DeleteLocalRef(env, jCls);
  213.  
  214.   _exceptionOcurred: jni_ExceptionOccurred(env);
  215. end;
  216.  
  217.  
  218. function jBarcodeGen_QRCode(env: PJNIEnv; _jbarcodegen: JObject; text: string; width: integer; height: integer): jObject;
  219. var
  220.   jParams: array[0..2] of jValue;
  221.   jMethod: jMethodID=nil;
  222.   jCls: jClass=nil;
  223. label
  224.   _exceptionOcurred;
  225. begin
  226.  
  227.   if (env = nil) or (_jbarcodegen = nil) then exit;
  228.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  229.   if jCls = nil then goto _exceptionOcurred;
  230.   jMethod:= env^.GetMethodID(env, jCls, 'QRCode', '(Ljava/lang/String;II)Landroid/graphics/Bitmap;');
  231.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  232.  
  233.   jParams[0].l:= env^.NewStringUTF(env, PChar(text));
  234.   jParams[1].i:= width;
  235.   jParams[2].i:= height;
  236.  
  237.  
  238.   Result:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  239. env^.DeleteLocalRef(env,jParams[0].l);
  240.  
  241.   env^.DeleteLocalRef(env, jCls);
  242.  
  243.   _exceptionOcurred: jni_ExceptionOccurred(env);
  244. end;
  245.  
  246.  
  247. function jBarcodeGen_Get128Bar(env: PJNIEnv; _jbarcodegen: JObject; _data: string; _width: integer; _height: integer): jObject;
  248. var
  249.   jParams: array[0..2] of jValue;
  250.   jMethod: jMethodID=nil;
  251.   jCls: jClass=nil;
  252. label
  253.   _exceptionOcurred;
  254. begin
  255.  
  256.   if (env = nil) or (_jbarcodegen = nil) then exit;
  257.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  258.   if jCls = nil then goto _exceptionOcurred;
  259.   jMethod:= env^.GetMethodID(env, jCls, 'Get128Bar', '(Ljava/lang/String;II)Landroid/graphics/Bitmap;');
  260.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  261.  
  262.   jParams[0].l:= env^.NewStringUTF(env, PChar(_data));
  263.   jParams[1].i:= _width;
  264.   jParams[2].i:= _height;
  265.  
  266.  
  267.   Result:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  268. env^.DeleteLocalRef(env,jParams[0].l);
  269.  
  270.   env^.DeleteLocalRef(env, jCls);
  271.  
  272.   _exceptionOcurred: jni_ExceptionOccurred(env);
  273. end;
  274.  
  275.  
  276. function jBarcodeGen_GetUPCAChecksum(env: PJNIEnv; _jbarcodegen: JObject; _data11digits: string): string;
  277. var
  278.   jStr: JString;
  279.   jBoo: JBoolean;
  280.   jParams: array[0..0] of jValue;
  281.   jMethod: jMethodID=nil;
  282.   jCls: jClass=nil;
  283. label
  284.   _exceptionOcurred;
  285. begin
  286.  
  287.   if (env = nil) or (_jbarcodegen = nil) then exit;
  288.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  289.   if jCls = nil then goto _exceptionOcurred;
  290.   jMethod:= env^.GetMethodID(env, jCls, 'GetUPCAChecksum', '(Ljava/lang/String;)Ljava/lang/String;');
  291.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  292.  
  293.   jParams[0].l:= env^.NewStringUTF(env, PChar(_data11digits));
  294.  
  295.  
  296.   jStr:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  297.  
  298.   Result:= GetPStringAndDeleteLocalRef(env, jStr);
  299. env^.DeleteLocalRef(env,jParams[0].l);
  300.  
  301.   env^.DeleteLocalRef(env, jCls);
  302.  
  303.   _exceptionOcurred: jni_ExceptionOccurred(env);
  304. end;
  305.  
  306.  
  307. function jBarcodeGen_GetUPCEChecksum(env: PJNIEnv; _jbarcodegen: JObject; _data7digits: string): string;
  308. var
  309.   jStr: JString;
  310.   jBoo: JBoolean;
  311.   jParams: array[0..0] of jValue;
  312.   jMethod: jMethodID=nil;
  313.   jCls: jClass=nil;
  314. label
  315.   _exceptionOcurred;
  316. begin
  317.  
  318.   if (env = nil) or (_jbarcodegen = nil) then exit;
  319.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  320.   if jCls = nil then goto _exceptionOcurred;
  321.   jMethod:= env^.GetMethodID(env, jCls, 'GetUPCEChecksum', '(Ljava/lang/String;)Ljava/lang/String;');
  322.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  323.  
  324.   jParams[0].l:= env^.NewStringUTF(env, PChar(_data7digits));
  325.  
  326.  
  327.   jStr:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  328.  
  329.   Result:= GetPStringAndDeleteLocalRef(env, jStr);
  330. env^.DeleteLocalRef(env,jParams[0].l);
  331.  
  332.   env^.DeleteLocalRef(env, jCls);
  333.  
  334.   _exceptionOcurred: jni_ExceptionOccurred(env);
  335. end;
  336.  
  337.  
  338. function jBarcodeGen_GetCode39Checksum(env: PJNIEnv; _jbarcodegen: JObject; _dataCode: string): string;
  339. var
  340.   jStr: JString;
  341.   jBoo: JBoolean;
  342.   jParams: array[0..0] of jValue;
  343.   jMethod: jMethodID=nil;
  344.   jCls: jClass=nil;
  345. label
  346.   _exceptionOcurred;
  347. begin
  348.  
  349.   if (env = nil) or (_jbarcodegen = nil) then exit;
  350.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  351.   if jCls = nil then goto _exceptionOcurred;
  352.   jMethod:= env^.GetMethodID(env, jCls, 'GetCode39Checksum', '(Ljava/lang/String;)Ljava/lang/String;');
  353.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  354.  
  355.   jParams[0].l:= env^.NewStringUTF(env, PChar(_dataCode));
  356.  
  357.  
  358.   jStr:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  359.  
  360.   Result:= GetPStringAndDeleteLocalRef(env, jStr);
  361. env^.DeleteLocalRef(env,jParams[0].l);
  362.  
  363.   env^.DeleteLocalRef(env, jCls);
  364.  
  365.   _exceptionOcurred: jni_ExceptionOccurred(env);
  366. end;
  367.  
  368.  
  369. function jBarcodeGen_GetEAN13Checksum(env: PJNIEnv; _jbarcodegen: JObject; _data12digits: string): string;
  370. var
  371.   jStr: JString;
  372.   jBoo: JBoolean;
  373.   jParams: array[0..0] of jValue;
  374.   jMethod: jMethodID=nil;
  375.   jCls: jClass=nil;
  376. label
  377.   _exceptionOcurred;
  378. begin
  379.  
  380.   if (env = nil) or (_jbarcodegen = nil) then exit;
  381.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  382.   if jCls = nil then goto _exceptionOcurred;
  383.   jMethod:= env^.GetMethodID(env, jCls, 'GetEAN13Checksum', '(Ljava/lang/String;)Ljava/lang/String;');
  384.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  385.  
  386.   jParams[0].l:= env^.NewStringUTF(env, PChar(_data12digits));
  387.  
  388.  
  389.   jStr:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  390.  
  391.   Result:= GetPStringAndDeleteLocalRef(env, jStr);
  392. env^.DeleteLocalRef(env,jParams[0].l);
  393.  
  394.   env^.DeleteLocalRef(env, jCls);
  395.  
  396.   _exceptionOcurred: jni_ExceptionOccurred(env);
  397. end;
  398.  
  399.  
  400. function jBarcodeGen_GetEAN8Checksum(env: PJNIEnv; _jbarcodegen: JObject; _data7digits: string): string;
  401. var
  402.   jStr: JString;
  403.   jBoo: JBoolean;
  404.   jParams: array[0..0] of jValue;
  405.   jMethod: jMethodID=nil;
  406.   jCls: jClass=nil;
  407. label
  408.   _exceptionOcurred;
  409. begin
  410.  
  411.   if (env = nil) or (_jbarcodegen = nil) then exit;
  412.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  413.   if jCls = nil then goto _exceptionOcurred;
  414.   jMethod:= env^.GetMethodID(env, jCls, 'GetEAN8Checksum', '(Ljava/lang/String;)Ljava/lang/String;');
  415.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  416.  
  417.   jParams[0].l:= env^.NewStringUTF(env, PChar(_data7digits));
  418.  
  419.  
  420.   jStr:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  421.  
  422.   Result:= GetPStringAndDeleteLocalRef(env, jStr);
  423. env^.DeleteLocalRef(env,jParams[0].l);
  424.  
  425.   env^.DeleteLocalRef(env, jCls);
  426.  
  427.   _exceptionOcurred: jni_ExceptionOccurred(env);
  428. end;
  429.  
  430.  
  431. function jBarcodeGen_GetBar1D(env: PJNIEnv; _jbarcodegen: JObject; _format: integer; _data: string; _width: integer; _height: integer): jObject;
  432. var
  433.   jParams: array[0..3] of jValue;
  434.   jMethod: jMethodID=nil;
  435.   jCls: jClass=nil;
  436. label
  437.   _exceptionOcurred;
  438. begin
  439.  
  440.   if (env = nil) or (_jbarcodegen = nil) then exit;
  441.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  442.   if jCls = nil then goto _exceptionOcurred;
  443.   jMethod:= env^.GetMethodID(env, jCls, 'GetBar1D', '(ILjava/lang/String;II)Landroid/graphics/Bitmap;');
  444.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  445.  
  446.   jParams[0].i:= _format;
  447.   jParams[1].l:= env^.NewStringUTF(env, PChar(_data));
  448.   jParams[2].i:= _width;
  449.   jParams[3].i:= _height;
  450.  
  451.  
  452.   Result:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  453. env^.DeleteLocalRef(env,jParams[1].l);
  454.  
  455.   env^.DeleteLocalRef(env, jCls);
  456.  
  457.   _exceptionOcurred: jni_ExceptionOccurred(env);
  458. end;
  459.  
  460.  
  461. function jBarcodeGen_GetBar2D(env: PJNIEnv; _jbarcodegen: JObject; _data: string; _width: integer; _height: integer; _format: integer): jObject;
  462. var
  463.   jParams: array[0..3] of jValue;
  464.   jMethod: jMethodID=nil;
  465.   jCls: jClass=nil;
  466. label
  467.   _exceptionOcurred;
  468. begin
  469.  
  470.   if (env = nil) or (_jbarcodegen = nil) then exit;
  471.   jCls:= env^.GetObjectClass(env, _jbarcodegen);
  472.   if jCls = nil then goto _exceptionOcurred;
  473.   jMethod:= env^.GetMethodID(env, jCls, 'GetBar2D', '(Ljava/lang/String;III)Landroid/graphics/Bitmap;');
  474.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  475.  
  476.   jParams[0].l:= env^.NewStringUTF(env, PChar(_data));
  477.   jParams[1].i:= _width;
  478.   jParams[2].i:= _height;
  479.   jParams[3].i:= _format;
  480.  
  481.  
  482.   Result:= env^.CallObjectMethodA(env, _jbarcodegen, jMethod, @jParams);
  483. env^.DeleteLocalRef(env,jParams[0].l);
  484.  
  485.   env^.DeleteLocalRef(env, jCls);
  486.  
  487.   _exceptionOcurred: jni_ExceptionOccurred(env);
  488. end;
  489.  
  490.  
  491.  
  492. end.
  493.  

Success creating Qrcode  :D

Thank you

answer:
https://repo1.maven.org/maven2/com/google/zxing/core/3.4.0/
https://repo1.maven.org/maven2/com/google/zxing/javase/3.4.0/
https://stackoverflow.com/questions/8800919/how-to-generate-a-qr-code-for-an-android-application/25283174#25283174
« Last Edit: September 17, 2023, 05:30:33 am by Mongkey »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2318
Re: LAMW - barcodegen QrCode creation addition
« Reply #2 on: October 24, 2023, 07:15:00 pm »
Great!

Done!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018