use of gov.usgs.cida.utilities.QRCodeGenerator in project coastal-hazards by USGS-CIDA.
the class QRCodeResource method generateQRImageUsingItemID.
/**
* Produces a QR code that directs to back of card for a given item ID
*
* @param id
* @param width
* @param height
* @return
*/
@GET
@Path("/info/item/{id}")
@Produces("image/png")
public Response generateQRImageUsingItemID(@PathParam("id") String id, @QueryParam("width") int width, @QueryParam("height") int height) throws IOException {
URL url = null;
String urlString = "ui/info/item/" + id;
Response response;
QRCodeGenerator qrcr = new QRCodeGenerator();
// Make sure the item exists in the database
try (ItemManager itemManager = new ItemManager()) {
Item item = itemManager.load(id);
if (item == null) {
throw new NotFoundException();
}
}
// Check if the base URL doesn't contain a trailing slash. If not, attach one to the beginning of url string
if (BASE_URL.charAt(BASE_URL.length() - 1) != '/') {
urlString = "/" + urlString;
}
// Create the URL string
urlString = BASE_URL + urlString;
try {
url = new URL(urlString);
qrcr.setUrl(url);
} catch (MalformedURLException | URISyntaxException ex) {
throw new ParamException.QueryParamException(ex, "URL could not be formed", "URL " + url + " could not be formed.");
}
if (width > 0 && height > 0) {
if (width > MAX_WIDTH) {
qrcr.setWidth(MAX_WIDTH);
} else {
qrcr.setWidth(width);
}
if (height > MAX_HEIGHT) {
qrcr.setHeight(MAX_HEIGHT);
} else {
qrcr.setHeight(height);
}
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
qrcr.writeToOutputStream(baos);
baos.flush();
response = Response.ok(baos.toByteArray(), "image/png").build();
}
return response;
}
Aggregations