Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples of reading the generated QR codes #16

Closed
jeroen opened this issue Oct 29, 2023 · 6 comments
Closed

Add examples of reading the generated QR codes #16

jeroen opened this issue Oct 29, 2023 · 6 comments

Comments

@jeroen
Copy link

jeroen commented Oct 29, 2023

The latest version of opencv can detect and decode QR codes: https://docs.ropensci.org/opencv/reference/qrcode.html

We could use this for unit tests or user examples of reading the generated QR back into R.

@ThierryO
Copy link
Owner

ThierryO commented Nov 7, 2023

That would certainly be an option. I've noticed that opencv suggests qrcode. Don't we risk to get a circular dependency when qrcode suggests on opencv?

@jeroen
Copy link
Author

jeroen commented Nov 7, 2023

That only is a problem for hard dependencies, like "Imports". But "Suggests" is a soft dependency, so either package can be loaded without the other, so there is no problem.

@ThierryO
Copy link
Owner

ThierryO commented Nov 7, 2023

I wrote a little test function. It works with ascii but fails with special characters.

  test_read_qr <- function(input) {
    requireNamespace("opencv")
    test_file <- tempfile("qrcode", fileext = ".png")
    png(test_file)
    plot(qr_code(input))
    dev.off()
    opencv::ocv_read(test_file) |>
      opencv::ocv_qr_detect() |>
      expect_equal(input)
    rm(test_file)
  }
test_read_qr("\u00E6")
x <- "fa\xE7ile"
Encoding(x) <- "latin1"
test_read_qr(x)
Error: opencv::ocv_qr_detect(opencv::ocv_read(test_file)) (`actual`) not equal to `input` (`expected`).

`actual`:   "\xe6"
`expected`: "æ"   
In addition: Warning messages:
1: In grepl("\n", x) : unable to translate '<e6>' to a wide string
2: In grepl("\n", x) : input string 1 is invalid

Error: opencv::ocv_qr_detect(opencv::ocv_read(test_file)) (`actual`) not equal to `input` (`expected`).

`actual`:   "fa\xe7ile"
`expected`: "façile"   
In addition: Warning messages:
1: In grepl("\n", x) : unable to translate 'fa<e7>ile' to a wide string
2: In grepl("\n", x) : input string 1 is invalid

@ThierryO
Copy link
Owner

ThierryO commented Nov 8, 2023

Below are the problematic qr codes. They give the correct output on my phone.

façile
qr code with content "façile"

æ
qr code with content "æ"

@jeroen
Copy link
Author

jeroen commented Nov 8, 2023

Good catch.

I see that you explicitly convert all input to latin in https://github.com/ThierryO/qrcode/blob/60b002480f137c305a9cd93b9acad16c3628f471/R/qr_encode.R#L61C1-L66

However latin1 is a legacy encoding that only works for european text and does not support Chineese, emojis, etc.

Most widely used qr encoders/decoders assume text is UTF-8 encoded from what I read from here.

I think the best solution is to update qrcode to always call enc2uf8 on the input text and not convert to latin1, such that the qr always contains the text in the standard utf-8 format.

@ThierryO
Copy link
Owner

ThierryO commented Nov 9, 2023

Handling UTF-8 as UTF-8 is not as simple as just using enc2utf8. I'm on a tight schedule due to a CRAN error. I'll use opencv only to test ASCII text.

ThierryO pushed a commit that referenced this issue Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants