#java #spring
#Ява #весна
Вопрос:
Я работаю над приложением для бронирования офисных столов, в котором пользователь бронирует офисный стол с помощью QR-кода. Со следующим кодом я могу сгенерировать QR-код в весенней загрузке, однако мне нужна помощь с кодом, чтобы использовать файл .png в качестве строки и передать его в базу данных(используя JDBC через MySQL). Любое другое предложение с изображением было бы действительно оценено по достоинству. Прикрепляю свою структуру папок тоже,где генерируется QR-код, PNG. Заранее спасибо.
В репозитории у меня есть QRCodeGenerator:
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } public static byte[] getQRCodeImage(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(); MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream); return pngOutputStream.toByteArray(); } }
Мой QR-контроллер выглядит так:
[import com.example.deskbooking.QRCodeGenerator; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController class QRCodeController { private static final String QR_CODE_IMAGE_PATH = "./src/main/resources/QRCode.png"; @GetMapping(value = "/genrateAndDownloadQRCode/{codeText}/{width}/{height}") public void download( @PathVariable("codeText") String codeText, @PathVariable("width") Integer width, @PathVariable("height") Integer height) throws Exception { QRCodeGenerator.generateQRCodeImage(codeText, width, height, QR_CODE_IMAGE_PATH); } @GetMapping(value = "/genrateQRCode/{codeText}/{width}/{height}") public ResponseEntitylt;byte[]gt; generateQRCode( @PathVariable("codeText") String codeText, @PathVariable("width") Integer width, @PathVariable("height") Integer height) throws Exception { return ResponseEntity.status(HttpStatus.OK).body(QRCodeGenerator.getQRCodeImage(codeText, width, height)); } }][1]