-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added QR code generator for device flow
- Loading branch information
Showing
6 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package oidc.qr; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class QRCode { | ||
|
||
private String url; | ||
private String image; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package oidc.qr; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
import com.google.zxing.MultiFormatWriter; | ||
import com.google.zxing.client.j2se.MatrixToImageWriter; | ||
import com.google.zxing.common.BitMatrix; | ||
import lombok.SneakyThrows; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.Base64; | ||
|
||
public class QRGenerator { | ||
|
||
private static final int IMAGE_SIZE = 400; | ||
|
||
private QRGenerator() { | ||
} | ||
|
||
@SneakyThrows | ||
public static QRCode qrCode(String url) { | ||
BitMatrix matrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, IMAGE_SIZE, IMAGE_SIZE); | ||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
MatrixToImageWriter.writeToStream(matrix, "png", bos); | ||
//<img src="data:image/png;base64,iVBORw0KGgoA....> | ||
String image = Base64.getEncoder().encodeToString(bos.toByteArray()); // base64 encode | ||
return new QRCode(url, image); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package oidc.qr; | ||
|
||
import com.google.zxing.BinaryBitmap; | ||
import com.google.zxing.MultiFormatReader; | ||
import com.google.zxing.Result; | ||
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; | ||
import com.google.zxing.common.HybridBinarizer; | ||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.io.ByteArrayInputStream; | ||
import java.util.Base64; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class QRGeneratorTest { | ||
|
||
@SneakyThrows | ||
@Test | ||
void qrCode() { | ||
String url = "https://surf.nl"; | ||
QRCode qrCode = QRGenerator.qrCode(url); | ||
assertEquals(url, qrCode.getUrl()); | ||
//Because we can | ||
byte[] decoded = Base64.getDecoder().decode(qrCode.getImage()); | ||
BinaryBitmap binaryBitmap = new BinaryBitmap( | ||
new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new ByteArrayInputStream(decoded))))); | ||
int height = binaryBitmap.getHeight(); | ||
int width = binaryBitmap.getWidth(); | ||
assertEquals(400, width); | ||
assertEquals(400, height); | ||
|
||
Result result = new MultiFormatReader().decode(binaryBitmap); | ||
assertEquals(url, result.getText()); | ||
} | ||
} |