createbarcode.lpr 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. program createbarcode;
  2. {$mode objfpc}
  3. {$H+}
  4. uses
  5. Classes, SysUtils, CustApp, fpbarcode, fpimgbarcode, fpimage,
  6. fpwritepng, fpwritebmp,fpwritejpeg,FPWritePNM,fpwritexpm;
  7. type
  8. { TCreateBarcodeApplication }
  9. TCreateBarcodeApplication = class(TCustomApplication)
  10. Private
  11. FWidth : Cardinal;
  12. FHeight : Cardinal;
  13. FUnit : Cardinal;
  14. FWeight : Double;
  15. FText : string;
  16. FFileName : String;
  17. FEncoding : TBarcodeEncoding;
  18. flist : Boolean;
  19. protected
  20. procedure DoRun; override;
  21. Procedure ListEncodings;
  22. Procedure CreateBarCode;
  23. public
  24. constructor Create(TheOwner: TComponent); override;
  25. procedure WriteHelp(S: String); virtual;
  26. Procedure AnalyzeParams;
  27. end;
  28. { TCreateBarcodeApplication }
  29. procedure TCreateBarcodeApplication.DoRun;
  30. begin
  31. AnalyzeParams;
  32. if FList then
  33. ListEncodings
  34. else
  35. CreateBarCode;
  36. Terminate;
  37. end;
  38. procedure TCreateBarcodeApplication.ListEncodings;
  39. Var
  40. E : TBarcodeEncoding;
  41. S : String;
  42. begin
  43. Writeln('Known encodings : ');
  44. For E in TBarcodeEncoding do
  45. begin
  46. Str(E,S);
  47. Delete(S,1,2);
  48. Writeln(S:16,': ',BarcodeEncodingNames[E]);
  49. end;
  50. end;
  51. procedure TCreateBarcodeApplication.CreateBarCode;
  52. Var
  53. Img : TFPCustomImage;
  54. begin
  55. Img:=TFPCompactImgGray16Bit.Create(FWidth,FHeight);
  56. try
  57. DrawBarCode(Img,FText,FEncoding,FUnit,FWeight);
  58. Writeln('Writing to file : ',FFilename);
  59. Img.SaveToFile(FFileName);
  60. finally
  61. Img.Free;
  62. end;
  63. end;
  64. constructor TCreateBarcodeApplication.Create(TheOwner: TComponent);
  65. begin
  66. inherited Create(TheOwner);
  67. StopOnException:=True;
  68. end;
  69. procedure TCreateBarcodeApplication.WriteHelp(S : String);
  70. begin
  71. if (S<>'') then
  72. Writeln('Error : ',S);
  73. writeln('Usage: ', ExeName, ' -h');
  74. Free;
  75. Halt(Ord(S<>''));
  76. end;
  77. procedure TCreateBarcodeApplication.AnalyzeParams;
  78. Var
  79. S,ES : String;
  80. E : TBarcodeEncoding;
  81. B : Boolean;
  82. begin
  83. S:=CheckOptions('hw:h:t:o:lu:i:e:', ['help','width:','height:','text:','encoding','output:','list','unit:','weight:']);
  84. if (S<>'') or HasOption('h', 'help') then
  85. WriteHelp(S);
  86. if HasOption('h','height') then
  87. FHeight:=StrToInt(GetOptionValue('h','height'));
  88. if HasOption('w','width') then
  89. FWidth:=StrToInt(GetOptionValue('w','width'));
  90. if HasOption('u','unit') then
  91. FUnit:=StrToInt(GetOptionValue('u','unit'));
  92. if HasOption('i','weight') then
  93. FWeight:=StrToFloat(GetOptionValue('i','weight'));
  94. FText:=GetOptionValue('t','text');
  95. FFileName:=GetOptionValue('o','output');
  96. S:=GetOptionValue('e','encoding');
  97. FList:=HasOption('l','list');
  98. if FList then
  99. exit;
  100. // Sanitize
  101. if (S='') then
  102. WriteHelp('Need barcode encoding');
  103. if FText='' then
  104. WriteHelp('Need a text');
  105. E:=Low(TBarCodeEncoding);
  106. B:=False;
  107. While (Not B) and (E<=High(TBarcodeEncoding)) do
  108. begin
  109. Str(E,ES);
  110. delete(ES,1,2);
  111. B:=SameText(S,ES);
  112. if B then
  113. FEncoding:=E;
  114. E:=Succ(E);
  115. end;
  116. if not B then
  117. WriteHelp('Invalid barcode encoding: '+S);
  118. if FWidth=0 then
  119. FWidth:=200;
  120. if Fheight=0 then
  121. FHeight:=30;
  122. if FUnit=0 then
  123. FUnit:=1;
  124. if FWeight=0 then
  125. FWeight:=2.0;
  126. end;
  127. var
  128. Application: TCreateBarcodeApplication;
  129. begin
  130. Application:=TCreateBarcodeApplication.Create(nil);
  131. Application.Title:='Create Barcodes';
  132. Application.Run;
  133. Application.Free;
  134. end.