Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Created September 9, 2023 16:25
Show Gist options
  • Save mayankchoubey/337e51bb52bf6c9491867c9b5fa75101 to your computer and use it in GitHub Desktop.
Save mayankchoubey/337e51bb52bf6c9491867c9b5fa75101 to your computer and use it in GitHub Desktop.
SpringBoot - QR generator API
server.port=3000
server.ssl.certificate=/Users/mayankc/Work/source/certs/cert.pem
server.ssl.certificate-private-key=/Users/mayankc/Work/source/certs/key.pem
package com.example.qr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.context.annotation.Bean;
import java.util.concurrent.Executors;
@SpringBootApplication
public class QrApplication {
public static void main(String[] args) {
SpringApplication.run(QrApplication.class, args);
}
@Bean
public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
return protocolHandler -> {
protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
};
}
}
package com.example.qr;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
import com.example.qr.QrRequest;
import com.example.qr.QrGenerator;
@RestController
public class QrController {
@PostMapping("/qr")
public ResponseEntity handleRequest(@RequestBody QrRequest qrRequest) {
if(qrRequest.getUrlToEmbed() == null) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
try {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_TYPE, "image/png");
return new ResponseEntity<byte[]>(
QrGenerator.generateQR(qrRequest.getUrlToEmbed(), 512, 512),
httpHeaders,
HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
package com.example.qr;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class QrGenerator {
public static byte[] generateQR(String text, int width, int height) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
MatrixToImageConfig con = new MatrixToImageConfig() ;
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream, con);
byte[] pngData = pngOutputStream.toByteArray();
return pngData;
}
}
package com.example.qr;
public class QrRequest {
private String urlToEmbed;
public String getUrlToEmbed() {
return this.urlToEmbed;
}
public void setUrlToEmbed(String urlToEmbed) {
this.urlToEmbed = urlToEmbed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment