This commit is contained in:
Bill Burke 2014-04-25 10:02:49 -04:00
parent 541e865fe8
commit 1de4c31750

View file

@ -1,56 +1,56 @@
package org.keycloak.services.resources; package org.keycloak.services.resources;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException; import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.QRCodeWriter;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput; import javax.ws.rs.core.StreamingOutput;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
@Path("/qrcode") @Path("/qrcode")
public class QRCodeResource { public class QRCodeResource {
@GET @GET
@Produces("image/png") @Produces("image/png")
public Response createQrCode(@QueryParam("contents") String contents, @QueryParam("size") String size) throws ServletException, IOException, WriterException { public Response createQrCode(@QueryParam("contents") String contents, @QueryParam("size") String size) throws ServletException, IOException, WriterException {
int width = 256; int width = 256;
int height = 256; int height = 256;
if (size != null) { if (size != null) {
String[] s = size.split("x"); String[] s = size.split("x");
width = Integer.parseInt(s[0]); width = Integer.parseInt(s[0]);
height = Integer.parseInt(s[1]); height = Integer.parseInt(s[1]);
} }
if (contents == null) { if (contents == null) {
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} }
QRCodeWriter writer = new QRCodeWriter(); QRCodeWriter writer = new QRCodeWriter();
final BitMatrix bitMatrix = writer.encode(contents, BarcodeFormat.QR_CODE, width, height); final BitMatrix bitMatrix = writer.encode(contents, BarcodeFormat.QR_CODE, width, height);
StreamingOutput stream = new StreamingOutput() { StreamingOutput stream = new StreamingOutput() {
@Override @Override
public void write(OutputStream os) throws IOException, public void write(OutputStream os) throws IOException,
WebApplicationException { WebApplicationException {
MatrixToImageWriter.writeToStream(bitMatrix, "png", os); MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
} }
}; };
return Response.ok(stream).build(); return Response.ok(stream).build();
} }
} }