简体   繁体   中英

It can't recognize the result of the QR Code reader as a string

I have a QR Code reader and when it scans a qr code and gets a text, I want the code to check that text with an if function, and display a certain image. But for some reason it always gets stuck in the if function... It's like it won't recognize the result.getText().ToString() as a string...\\

public void processPicture(CameraEvent event) {
        updateLayout("Please wait...");

        if (event.getIndex() == 0) {
            if (event.getData() != null && event.getData().length > 0) {
                byte[] data = event.getData();
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
                //copy pixel data from the Bitmap into the 'intArray' array
                bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

                LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);

                BinaryBitmap bbmap = new BinaryBitmap(new HybridBinarizer(source));
                Reader reader = new QRCodeReader();
                int DelayTime = 5000;
                boolean error = false;
                try {

                    Result result = reader.decode(bbmap);
                    Log.d(Constants.LOG_TAG, result.getText());
                    qc = result.getText().toString();

                   // updateLayout(result.getText());
                    if( qc == "zoi"){
                        Bundle iconBundle = new Bundle();
                        iconBundle.putInt(Control.Intents.EXTRA_LAYOUT_REFERENCE, R.id.image);
                        iconBundle.putString(Control.Intents.EXTRA_DATA_URI,
                                getUriString(R.drawable.zoiko));
                        currentlyTakingPicture = false;
                        showLayout(R.layout.layout, new Bundle[] {iconBundle});
                    }
                } catch (NotFoundException e) {
                    updateLayout("QR Code Not Found");
                    error = true;
                    e.printStackTrace();
                } catch (ChecksumException e) {
                    e.printStackTrace();
                    updateLayout(new String[] {
                            "QR Code looks corrupted",
                            "Maybe try again?"
                    });
                    error = true;
                } catch (FormatException e) {
                    e.printStackTrace();
                    updateLayout("That's not a QR Code");
                    error = true;
                }

                if (error) {
                    try {
                        Thread.sleep(DelayTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    currentlyTakingPicture = false;
                    updateLayout(DEFAULT_TEXT);
                }
            }
        }
    }

I think you should use .equals() method for comparision with String. So your if condition would be -

if(result.getText().toString().equals("zoi")){
//your code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM