-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bar codes for credential offers on our openid4vci server page. (#780)
Signed-off-by: Peter Sorotokin <[email protected]>
- Loading branch information
Showing
4 changed files
with
58 additions
and
9 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
32 changes: 32 additions & 0 deletions
32
server-openid4vci/src/main/java/com/android/identity/server/openid4vci/QrServlet.kt
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,32 @@ | ||
package com.android.identity.server.openid4vci | ||
|
||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel | ||
import com.google.zxing.qrcode.encoder.Encoder | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import java.awt.image.BufferedImage | ||
import java.awt.image.DataBufferByte | ||
import javax.imageio.ImageIO | ||
|
||
/** | ||
* Servlet that encodes text string (passed as `q` parameter) into a QR code. | ||
*/ | ||
class QrServlet : BaseServlet() { | ||
override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) { | ||
val str = req.getParameter("q") ?: throw IllegalStateException("q parameter required") | ||
val qr = Encoder.encode(str, ErrorCorrectionLevel.L, null) | ||
val matrix = qr.matrix | ||
val width = matrix.width | ||
val height = matrix.height | ||
val image = BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY) | ||
val pixels = (image.raster.dataBuffer as DataBufferByte).data | ||
var index = 0 | ||
for (y in 0 until height) { | ||
for (x in 0 until width) { | ||
pixels[index++] = if (matrix[x, y].toInt() == 0) -1 else 0 | ||
} | ||
} | ||
resp.contentType = "image/png" | ||
ImageIO.write(image, "png", resp.outputStream) | ||
} | ||
} |
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