Skip to content

Commit 439ca79

Browse files
committed
image format param (gif, or png) default to png
1 parent e57b2b1 commit 439ca79

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

src/main/java/com/pnuema/java/barcode/barcodeapi/BarcodeBody.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class BarcodeBody {
77
private String type;
88
private String data;
9+
private String imageFormat;
910
private Integer w;
1011
private Integer h;
1112
private boolean label;
@@ -28,6 +29,18 @@ public void setData(String data) {
2829
this.data = data;
2930
}
3031

32+
public void setImageFormat(String imageFormat) {
33+
this.imageFormat = imageFormat;
34+
}
35+
36+
public Optional<String> getImageFormat() {
37+
if (imageFormat == null) {
38+
return Optional.empty();
39+
} else {
40+
return Optional.of(imageFormat);
41+
}
42+
}
43+
3144
public Optional<Integer> getW() {
3245
if (w == null)
3346
return Optional.empty();

src/main/java/com/pnuema/java/barcode/barcodeapi/controllers/v1/BarcodeController.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.http.MediaType;
99
import org.springframework.http.ResponseEntity;
1010
import org.springframework.lang.Nullable;
11+
import org.springframework.util.MimeTypeUtils;
1112
import org.springframework.web.bind.annotation.*;
1213

1314
import javax.imageio.ImageIO;
@@ -18,6 +19,7 @@
1819
import java.net.InetAddress;
1920
import java.net.UnknownHostException;
2021
import java.util.Optional;
22+
import java.util.concurrent.atomic.AtomicReference;
2123

2224
@RestController
2325
public 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

Comments
 (0)