88import org .springframework .http .MediaType ;
99import org .springframework .http .ResponseEntity ;
1010import org .springframework .lang .Nullable ;
11+ import org .springframework .util .MimeTypeUtils ;
1112import org .springframework .web .bind .annotation .*;
1213
1314import javax .imageio .ImageIO ;
1819import java .net .InetAddress ;
1920import java .net .UnknownHostException ;
2021import java .util .Optional ;
22+ import java .util .concurrent .atomic .AtomicReference ;
2123
2224@ RestController
2325public class BarcodeController extends AbstractV1Resource {
@@ -27,6 +29,7 @@ public class BarcodeController extends AbstractV1Resource {
2729 public ResponseEntity <byte []> getBarcodeImage (
2830 @ PathVariable (name = "type" ) String type ,
2931 @ PathVariable (name = "data" ) String data ,
32+ @ RequestParam (name = "imageFormat" ) Optional <String > imageFormat ,
3033 @ RequestParam (name = "w" ) Optional <Integer > width ,
3134 @ RequestParam (name = "h" ) Optional <Integer > height ,
3235 @ RequestParam (name = "label" ) Optional <Boolean > includeLabel ,
@@ -36,6 +39,7 @@ public ResponseEntity<byte[]> getBarcodeImage(
3639 return generateBarcode (
3740 type ,
3841 data ,
42+ imageFormat ,
3943 width ,
4044 height ,
4145 includeLabel ,
@@ -53,6 +57,7 @@ public ResponseEntity<byte[]> getBarcodeImage(@RequestBody BarcodeBody body) thr
5357 return generateBarcode (
5458 body .getType (),
5559 body .getData (),
60+ body .getImageFormat (),
5661 body .getW (),
5762 body .getH (),
5863 body .isLabel (),
@@ -64,6 +69,7 @@ public ResponseEntity<byte[]> getBarcodeImage(@RequestBody BarcodeBody body) thr
6469 private ResponseEntity <byte []> generateBarcode (
6570 String type ,
6671 String data ,
72+ Optional <String > imageFormat ,
6773 Optional <Integer > width ,
6874 Optional <Integer > height ,
6975 Optional <Boolean > includeLabel ,
@@ -82,7 +88,22 @@ private ResponseEntity<byte[]> generateBarcode(
8288
8389 HttpHeaders responseHeaders = new HttpHeaders ();
8490
85- if (typeEnum == null ) {
91+ String imageFormatInternal ;
92+ MediaType imageFormatMimeType ;
93+ String imageFormatHeader = "" ;
94+ if (imageFormat .isPresent ()) {
95+ imageFormatInternal = imageFormat .get ();
96+ imageFormatMimeType = getImageFormat (imageFormatInternal );
97+ if (imageFormatMimeType != null ) {
98+ imageFormatHeader = getImageFormatHeader (imageFormatInternal );
99+ }
100+ } else {
101+ imageFormatInternal = "png" ;
102+ imageFormatHeader = MediaType .IMAGE_PNG_VALUE ;
103+ imageFormatMimeType = MediaType .IMAGE_PNG ;
104+ }
105+
106+ if (typeEnum == null || imageFormatMimeType == null || imageFormatHeader == null ) {
86107 return ResponseEntity
87108 .badRequest ()
88109 .headers (responseHeaders )
@@ -120,8 +141,8 @@ private ResponseEntity<byte[]> generateBarcode(
120141 return ResponseEntity
121142 .ok ()
122143 .headers (responseHeaders )
123- .contentType (MediaType . IMAGE_PNG )
124- .body (getImgBytes (image ));
144+ .contentType (imageFormatMimeType )
145+ .body (getImgBytes (image , imageFormatInternal ));
125146 }
126147
127148 private String getMachineName () {
@@ -172,14 +193,34 @@ private EncodingType convertTypeStringToEnum(String type) {
172193
173194 }
174195
175- private byte [] getImgBytes (BufferedImage image ) throws IOException {
196+ @ Nullable
197+ private MediaType getImageFormat (String imageFormat ) {
198+ return switch (imageFormat .toLowerCase ()) {
199+ case "gif" -> MediaType .IMAGE_GIF ;
200+ case "png" -> MediaType .IMAGE_PNG ;
201+ default -> null ;
202+ };
203+ }
204+
205+ @ Nullable
206+ private String getImageFormatHeader (String imageFormat ) {
207+ return switch (imageFormat .toLowerCase ()) {
208+ case "gif" -> MediaType .IMAGE_GIF_VALUE ;
209+ case "png" -> MediaType .IMAGE_PNG_VALUE ;
210+ default -> null ;
211+ };
212+ }
213+
214+ private byte [] getImgBytes (BufferedImage image , String imageFormat ) throws IOException {
176215 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
177- ImageIO .write (image , "PNG" , baos );
178- return baos .toByteArray ();
216+ ImageIO .write (image , imageFormat .toUpperCase (), baos );
217+ byte [] output = baos .toByteArray ();
218+ baos .close ();
219+ return output ;
179220 }
180221
181222 /**
182- *
223+ * Convert hex color code to a java.awt.Color
183224 * @param colorStr e.g. "FFFFFF" or "00FFFF"
184225 * @return {@link Color}
185226 */
0 commit comments