33import com .pnuema .java .barcode .Barcode ;
44import com .pnuema .java .barcode .EncodingType ;
55import com .pnuema .java .barcode .barcodeapi .BarcodeBody ;
6+ import io .swagger .v3 .oas .annotations .media .Schema ;
67import org .springframework .cache .annotation .Cacheable ;
78import org .springframework .http .HttpHeaders ;
89import org .springframework .http .MediaType ;
2223@ RestController
2324public class BarcodeController extends AbstractV1Resource {
2425
25- @ GetMapping (value = "/barcode/{type}/data/{data}" )
26- @ Cacheable ("barcodes" )
27- public ResponseEntity <byte []> getBarcodeImage (
28- @ PathVariable (name = "type" ) String type ,
29- @ PathVariable (name = "data" ) String data ,
30- @ RequestParam (name = "imageFormat" ) Optional <String > imageFormat ,
31- @ RequestParam (name = "w" ) Optional <Integer > width ,
32- @ RequestParam (name = "h" ) Optional <Integer > height ,
33- @ RequestParam (name = "label" ) Optional <Boolean > includeLabel ,
34- @ RequestParam (name = "barcolor" ) Optional <String > barColor ,
35- @ RequestParam (name = "background" ) Optional <String > background ) throws IOException {
36-
37- return generateBarcode (
38- type ,
39- data ,
40- imageFormat ,
41- width ,
42- height ,
43- includeLabel ,
44- barColor ,
45- background
46- );
47- }
48-
4926 @ PostMapping (value = "/barcode/" ,
50- consumes = {MediaType .APPLICATION_JSON_VALUE , MediaType .APPLICATION_XML_VALUE },
51- produces = {MediaType .APPLICATION_JSON_VALUE , MediaType .APPLICATION_XML_VALUE })
27+ consumes = { MediaType .APPLICATION_JSON_VALUE },
28+ produces = { MediaType .IMAGE_PNG_VALUE , MediaType .IMAGE_JPEG_VALUE , MediaType .IMAGE_GIF_VALUE }
29+ )
5230 @ Cacheable ("barcodes" )
5331 public ResponseEntity <byte []> getBarcodeImage (@ RequestBody BarcodeBody body ) throws IOException {
54-
5532 return generateBarcode (
5633 body .getType (),
5734 body .getData (),
@@ -64,6 +41,58 @@ public ResponseEntity<byte[]> getBarcodeImage(@RequestBody BarcodeBody body) thr
6441 );
6542 }
6643
44+ @ GetMapping (
45+ name = "barcode" ,
46+ value = "/barcode/{type}/data/{data}" ,
47+ produces = { MediaType .IMAGE_PNG_VALUE , MediaType .IMAGE_JPEG_VALUE , MediaType .IMAGE_GIF_VALUE }
48+ )
49+ @ Cacheable ("barcodes" )
50+ public ResponseEntity <byte []> getBarcodeImage (
51+ @ Schema (example = "upca" , implementation = EncodingType .class , defaultValue = "upca" )
52+ @ PathVariable (name = "type" )
53+ String type ,
54+
55+ @ Schema (example = "123456789012" )
56+ @ PathVariable (name = "data" )
57+ String data ,
58+
59+ @ Schema (example = "png" , allowableValues = { "png" , "jpg" , "gif" }, defaultValue = "png" )
60+ @ RequestParam (name = "imageFormat" )
61+ Optional <String > imageFormat ,
62+
63+ @ Schema (example = "400" )
64+ @ RequestParam (name = "w" )
65+ Optional <Integer > width ,
66+
67+ @ Schema (example = "200" )
68+ @ RequestParam (name = "h" )
69+ Optional <Integer > height ,
70+
71+ @ Schema (allowableValues = { "false" , "true" }, defaultValue = "false" )
72+ @ RequestParam (name = "label" , defaultValue = "false" )
73+ Optional <Boolean > includeLabel ,
74+
75+ @ Schema (example = "000000" )
76+ @ RequestParam (name = "barcolor" )
77+ Optional <String > barColor ,
78+
79+ @ Schema (example = "ffffff" )
80+ @ RequestParam (name = "background" )
81+ Optional <String > background
82+
83+ ) throws IOException {
84+ return generateBarcode (
85+ type ,
86+ data ,
87+ imageFormat ,
88+ width ,
89+ height ,
90+ includeLabel ,
91+ barColor ,
92+ background
93+ );
94+ }
95+
6796 private ResponseEntity <byte []> generateBarcode (
6897 String type ,
6998 String data ,
@@ -72,7 +101,8 @@ private ResponseEntity<byte[]> generateBarcode(
72101 Optional <Integer > height ,
73102 Optional <Boolean > includeLabel ,
74103 Optional <String > barColor ,
75- Optional <String > background ) throws IOException {
104+ Optional <String > background
105+ ) throws IOException {
76106
77107 Barcode barcode = new Barcode ();
78108
@@ -119,7 +149,11 @@ private ResponseEntity<byte[]> generateBarcode(
119149 //attach debug info to header
120150 responseHeaders .set ("x-barcode-version" , barcode .getTitle () + " " + barcode .getVersion ());
121151 responseHeaders .set ("x-raw-value" , barcode .getRawData ());
122- responseHeaders .set ("x-label-font" , barcode .getLabelFont ().getName ());
152+
153+ if (barcode .isIncludeLabel ()) {
154+ responseHeaders .set ("x-label-font" , barcode .getLabelFont ().getName ());
155+ }
156+
123157 responseHeaders .set ("x-served-by" , getMachineName ());
124158
125159 if (exception != null || image == null ) {
0 commit comments