Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How remove empty space

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How remove empty space

    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;
            }
          }
    Last edited by pingusteam; September 17th, 2018 at 01:47 PM.

  2. #2
    Junior Member
    Join Date
    Dec 2017
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How remove empty space

    I create program in java with netbeans 8.2. JDK its 1.8. for showing text I use jtextarea (I use this for input text). If I write text like; sample text with space .
    and I use comand
    string besedilo;
    besedilo = myvnos.getText( );
    beseda2 =besedilo.replaceAll(" ", "");
    myvnos.setText(beseda2);
    I get text without spaces like: sampletextwithspace.
    But uf I open QR code I cant remove empty space

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,325
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: How remove empty space

    open QR code I cant remove empty space
    Please post the code that isn't working. Add two print statements that print the value of the String being worked on - one before the code that is supposed to remove the spaces and one after the code that is supposed to removed the spaces.
    Execute the code, copy what is printed and pasted it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with empty arrays
    By robin_ in forum Loops & Control Statements
    Replies: 12
    Last Post: March 23rd, 2013, 11:00 AM
  2. Why am I getting an empty window here
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 17th, 2012, 07:33 AM
  3. My spinner is empty
    By VagosM in forum Android Development
    Replies: 0
    Last Post: November 28th, 2012, 04:28 PM
  4. Empty If Statement
    By lightOfDay in forum Loops & Control Statements
    Replies: 5
    Last Post: June 14th, 2012, 08:41 AM
  5. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM