first im creating program for encrypted text text its encrypted with base64. After then its text save to QR Code. When I now try open QR code I get encrypted text with empty spaces. How I remove empty space? Im try all I find on net but nothing vork normal. If I write own text it OK but if I ošem QR code doesnt work
str.replaceAll("\\s", "");
str.replace(" ", "");
code for creating QR code
Charset charset = Charset.forName("UTF-8"); CharsetEncoder encoder = charset.newEncoder(); byte[] b = null; try { // Convert a string to UTF-8 bytes in a ByteBuffer //ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("utf 8 characters - i used hebrew, but you should write some of your own language characters")); ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(besedilo)); b = bbuf.array(); } catch (CharacterCodingException e) { System.out.println(e.getMessage()); } String data; try { data = new String(b, "UTF-8"); // get a byte matrix for the data BitMatrix matrix = null; int h = 100; int w = 100; com.google.zxing.Writer writer = new MultiFormatWriter(); try { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints); } catch (com.google.zxing.WriterException e) { System.out.println(e.getMessage()); } // change this path to match yours (this is my mac home folder, you can use: c:\\qr_png.png if you are on windows) String filePath = beseda2; File file = new File(filePath); try { MatrixToImageWriter.writeToFile(matrix, "PNG", file); System.out.println("printing to " + file.getAbsolutePath()); } catch (IOException e) { System.out.println(e.getMessage()); } } catch (UnsupportedEncodingException e) { System.out.println(e.getMessage()); }
code for reading QR code
try { File file = new File(spomin); // spomin its path for file String decodedText = decodeQRCode(file); if(decodedText == null) { //System.out.println("No QR Code found in the image"); spomin= "NE NAJDEM QR KODE"; } else { //System.out.println("Sporočilo je = " + decodedText); spomin= decodedText; String result = decodedText.trim(); spomin= result; } } catch (IOException e) { System.out.println("Could not decode QR Code, IOException :: " + e.getMessage()); spomin= "NE morem prebrati QR KODE"; }
private static String decodeQRCode(File qrCodeimage) throws IOException { String aaa; BufferedImage bufferedImage = ImageIO.read(qrCodeimage); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { Result result = new MultiFormatReader().decode(bitmap); return result.getText(); } catch (NotFoundException e) { //aaa ="There is no QR code in the image"; return null; } }