feat(qrcode): Remove decoding QR code from an image

feat(qrcode): Remove decoding QR code from an image
This commit is contained in:
Ivan Enderlin
2022-09-12 13:33:42 +02:00
committed by GitHub
10 changed files with 112 additions and 455 deletions

307
Cargo.lock generated
View File

@@ -127,8 +127,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87bf87e6e8b47264efa9bde63d6225c6276a52e05e91bf37eaa8afd0032d6b71"
dependencies = [
"askama_shared",
"proc-macro2 1.0.43",
"syn 1.0.99",
"proc-macro2",
"syn",
]
[[package]]
@@ -147,10 +147,10 @@ dependencies = [
"mime",
"mime_guess",
"nom",
"proc-macro2 1.0.43",
"quote 1.0.21",
"proc-macro2",
"quote",
"serde",
"syn 1.0.99",
"syn",
"toml",
]
@@ -212,9 +212,9 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f203db73a71dfa2fb6dd22763990fa26f3d2625a6da2da900d23b87d26be27"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -223,9 +223,9 @@ version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76464446b8bc32758d7e88ee1a804d9914cd9b1cb264c029899680b0be29826f"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -573,9 +573,9 @@ checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -653,9 +653,9 @@ version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef196d5d972878a48da7decb7686eded338b4858fbabeed513d63a7c98b2b82d"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"unicode-xid 0.2.3",
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
@@ -834,8 +834,8 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdffe87e1d521a10f9696f833fe502293ea446d7f256c06128293a4119bdf4cb"
dependencies = [
"quote 1.0.21",
"syn 1.0.99",
"quote",
"syn",
]
[[package]]
@@ -879,10 +879,10 @@ checksum = "649c91bc01e8b1eac09fb91e8dbc7d517684ca6be8ebc75bb9cafc894f9fdb6f"
dependencies = [
"fnv",
"ident_case",
"proc-macro2 1.0.43",
"quote 1.0.21",
"proc-macro2",
"quote",
"strsim",
"syn 1.0.99",
"syn",
]
[[package]]
@@ -892,8 +892,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc69c5bfcbd2fc09a0f38451d2daf0e372e367986a83906d1b0dbc88134fb5"
dependencies = [
"darling_core",
"quote 1.0.21",
"syn 1.0.99",
"quote",
"syn",
]
[[package]]
@@ -903,7 +903,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "907076dfda823b0b36d2a1bb5f90c96660a5bbcd7729e10727f07858f22c4edc"
dependencies = [
"cfg-if",
"hashbrown 0.12.3",
"hashbrown",
"lock_api",
"once_cell",
"parking_lot_core 0.9.3",
@@ -972,9 +972,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f91d4cfa921f1c05904dc3c57b4a32c38aed3340cce209f3a6fd1478babafc4"
dependencies = [
"darling",
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -984,7 +984,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f0314b72bed045f3a68671b3c86328386762c93f82d98c65c3cb5e5f573dd68"
dependencies = [
"derive_builder_core",
"syn 1.0.99",
"syn",
]
[[package]]
@@ -1060,9 +1060,9 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bf95dc3f046b9da4f2d51833c0d3547d8564ef6910f5c1ed130306a75b92886"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -1292,9 +1292,9 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5129068fe3183546eaa0529af88ab0afbcddec2a373db69e94a20b8d5f6c4d74"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -1480,9 +1480,9 @@ version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42cd15d1c7456c04dbdf7e88bcd69760d74f3a798d6444e16974b505b0e62f17"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -1543,34 +1543,6 @@ dependencies = [
"byteorder",
]
[[package]]
name = "g2gen"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fc100b16c63808c5c388cd23ff94c5a35cf28ea459f336323f7948a39480555"
dependencies = [
"g2poly",
"proc-macro2 0.4.30",
"quote 0.6.13",
"syn 0.15.44",
]
[[package]]
name = "g2p"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf09bc632629cbe5420b330e45bcc8f80403e74ba1027d213258914fd5c62755"
dependencies = [
"g2gen",
"g2poly",
]
[[package]]
name = "g2poly"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e837767888fca507f07e89c90e0b350da7bbb89170f67a4655dc9bdc4cca457b"
[[package]]
name = "generic-array"
version = "0.14.6"
@@ -1665,15 +1637,6 @@ version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
dependencies = [
"ahash",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
@@ -1931,7 +1894,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"hashbrown 0.12.3",
"hashbrown",
"serde",
]
@@ -2112,22 +2075,13 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "lru"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ea2d928b485416e8908cff2d97d621db22b27f7b3b6729e438bcf42c671ba91"
dependencies = [
"hashbrown 0.11.2",
]
[[package]]
name = "lru"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999beba7b6e8345721bd280141ed958096a2e4abdf74f67ff4ce49b4b54e47a"
dependencies = [
"hashbrown 0.12.3",
"hashbrown",
]
[[package]]
@@ -2240,7 +2194,7 @@ dependencies = [
"futures-signals",
"futures-util",
"http",
"lru 0.7.8",
"lru",
"matrix-sdk-common",
"matrix-sdk-crypto",
"matrix-sdk-test",
@@ -2451,7 +2405,6 @@ dependencies = [
"byteorder",
"image 0.23.14",
"qrcode",
"rqrr",
"ruma-common",
"thiserror",
"vodozemac",
@@ -2522,9 +2475,9 @@ dependencies = [
name = "matrix-sdk-test-macros"
version = "0.2.0"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -2638,9 +2591,9 @@ source = "git+https://github.com/Hywan/napi-rs?branch=fix-napi-strict-on-t-and-r
dependencies = [
"convert_case",
"napi-derive-backend",
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -2650,10 +2603,10 @@ source = "git+https://github.com/Hywan/napi-rs?branch=fix-napi-strict-on-t-and-r
dependencies = [
"convert_case",
"once_cell",
"proc-macro2 1.0.43",
"quote 1.0.21",
"proc-macro2",
"quote",
"regex",
"syn 1.0.99",
"syn",
]
[[package]]
@@ -2870,9 +2823,9 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -3004,9 +2957,9 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -3152,9 +3105,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
"version_check",
]
@@ -3164,20 +3117,11 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
dependencies = [
"unicode-xid 0.1.0",
]
[[package]]
name = "proc-macro2"
version = "1.0.43"
@@ -3222,9 +3166,9 @@ checksum = "7b670f45da57fb8542ebdbb6105a925fe571b67f9e7ed9f47a06a84e72b4e7cc"
dependencies = [
"anyhow",
"itertools",
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -3263,22 +3207,13 @@ dependencies = [
"memchr",
]
[[package]]
name = "quote"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
dependencies = [
"proc-macro2 0.4.30",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2 1.0.43",
"proc-macro2",
]
[[package]]
@@ -3523,17 +3458,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "rqrr"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fa79947f53b20adb909a323d828d0fd744fa9d854792df07913b083bcd4d63b"
dependencies = [
"g2p",
"image 0.23.14",
"lru 0.6.6",
]
[[package]]
name = "ruma"
version = "0.6.3"
@@ -3631,11 +3555,11 @@ source = "git+https://github.com/ruma/ruma?rev=c745d3baf720b38a254e640a526717864
dependencies = [
"once_cell",
"proc-macro-crate",
"proc-macro2 1.0.43",
"quote 1.0.21",
"proc-macro2",
"quote",
"ruma-identifiers-validation",
"serde",
"syn 1.0.99",
"syn",
"toml",
]
@@ -3709,9 +3633,9 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "107c3d5d7f370ac09efa62a78375f94d94b8a33c61d8c278b96683fb4dbf2d8d"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -3781,9 +3705,9 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdbda6ac5cd1321e724fa9cee216f3a61885889b896f073b8f82322789c5250e"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -3862,9 +3786,9 @@ version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -4096,25 +4020,14 @@ dependencies = [
"symbolic-common",
]
[[package]]
name = "syn"
version = "0.15.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
dependencies = [
"proc-macro2 0.4.30",
"quote 0.6.13",
"unicode-xid 0.1.0",
]
[[package]]
name = "syn"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"proc-macro2",
"quote",
"unicode-ident",
]
@@ -4124,10 +4037,10 @@ version = "0.12.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"unicode-xid 0.2.3",
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[package]]
@@ -4204,9 +4117,9 @@ version = "1.0.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8f2591983642de85c921015f3f070c665a197ed69e417af436115e3a1407487"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -4301,9 +4214,9 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -4412,9 +4325,9 @@ version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11c75893af559bc8e10716548bdef5cb2b983f8e637db9d0e15126b61b484ee2"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -4511,12 +4424,6 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
[[package]]
name = "unicode-xid"
version = "0.2.3"
@@ -4580,10 +4487,10 @@ dependencies = [
"camino",
"fs-err",
"once_cell",
"proc-macro2 1.0.43",
"quote 1.0.21",
"proc-macro2",
"quote",
"serde",
"syn 1.0.99",
"syn",
"toml",
"uniffi_build",
"uniffi_meta",
@@ -4785,9 +4692,9 @@ dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
@@ -4809,7 +4716,7 @@ version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b260f13d3012071dfb1512849c033b1925038373aea48ced3012c09df952c602"
dependencies = [
"quote 1.0.21",
"quote",
"wasm-bindgen-macro-support",
]
@@ -4819,9 +4726,9 @@ version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be8e654bdd9b79216c2929ab90721aa82faf65c48cdf08bdc4e7f51357b80da"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@@ -4852,8 +4759,8 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6150d36a03e90a3cf6c12650be10626a9902d70c5270fd47d7a47e5389a10d56"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"proc-macro2",
"quote",
]
[[package]]
@@ -5090,8 +4997,8 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f8f187641dad4f680d25c4bfc4225b418165984179f26ca76ec4fb6441d3a17"
dependencies = [
"proc-macro2 1.0.43",
"quote 1.0.21",
"syn 1.0.99",
"proc-macro2",
"quote",
"syn",
"synstructure",
]

View File

@@ -15,21 +15,17 @@ repository = "https://github.com/matrix-org/matrix-rust-sdk"
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[features]
default = ["decode_image"]
decode_image = ["dep:image", "dep:rqrr", "qrcode/image", "qrcode/svg"]
[dependencies]
base64 = "0.13.0"
byteorder = "1.4.3"
# image and rqrr can't be upgraded before qrcode gets updated
# https://github.com/matrix-org/matrix-rust-sdk/issues/690
image = { version = "0.23.0", optional = true }
qrcode = { version = "0.12.0", default-features = false }
rqrr = { version = "0.4.0", optional = true }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "c745d3baf720b38a254e640a526717864e87a065" }
thiserror = "1.0.30"
[dependencies.vodozemac]
git = "https://github.com/matrix-org/vodozemac/"
rev = "ad1f098aff2a0b69aa5822c4e0679089a9fa4a86"
[dev-dependencies]
image = "0.23.0"
qrcode = { version = "0.12.0", default-features = false, features = ["image"] }

View File

@@ -6,21 +6,7 @@ interactive verification using [QR codes] in Matrix.
This is probably not the crate you are looking for, it's used internally in the
[matrix-sdk].
If you still want to play with QR codes, here are a couple of helpful examples.
### Decode an image
```rust,no_run
use image;
use matrix_sdk_qrcode::{QrVerificationData, DecodingError};
fn main() -> Result<(), DecodingError> {
let image = image::open("/path/to/my/image.png").unwrap();
let result = QrVerificationData::from_image(image)?;
Ok(())
}
```
If you still want to play with QR codes, here is a helpful example.
### Encode into a QR code

View File

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

View File

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

View File

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

View File

@@ -17,10 +17,6 @@ use thiserror::Error;
/// Error type describing errors that happen while QR data is being decoded.
#[derive(Error, Debug)]
pub enum DecodingError {
/// Error decoding the QR code.
#[cfg(feature = "decode_image")]
#[error(transparent)]
Qr(#[from] rqrr::DeQRError),
/// The QR code data is missing the mandatory Matrix header.
#[error("the decoded QR code is missing the Matrix header")]
Header,

View File

@@ -21,126 +21,15 @@ mod types;
mod utils;
pub use error::{DecodingError, EncodingError};
#[cfg(feature = "decode_image")]
pub use image;
pub use qrcode;
#[cfg(feature = "decode_image")]
pub use rqrr;
pub use types::{
QrVerificationData, SelfVerificationData, SelfVerificationNoMasterKey, VerificationData,
};
#[cfg(test)]
mod tests {
#[cfg(feature = "decode_image")]
use std::io::Cursor;
#[cfg(feature = "decode_image")]
use image::{ImageFormat, Luma};
#[cfg(feature = "decode_image")]
use qrcode::QrCode;
#[cfg(feature = "decode_image")]
use crate::utils::decode_qr;
use crate::{DecodingError, QrVerificationData};
#[cfg(feature = "decode_image")]
static VERIFICATION: &[u8; 4277] = include_bytes!("../data/verification.png");
#[cfg(feature = "decode_image")]
static SELF_VERIFICATION: &[u8; 1467] = include_bytes!("../data/self-verification.png");
#[cfg(feature = "decode_image")]
static SELF_NO_MASTER: &[u8; 1775] = include_bytes!("../data/self-no-master.png");
#[test]
#[cfg(feature = "decode_image")]
fn decode_qr_test() {
let image = Cursor::new(VERIFICATION);
let image = image::load(image, ImageFormat::Png).unwrap().to_luma8();
decode_qr(image).expect("Couldn't decode the QR code");
}
#[test]
#[cfg(feature = "decode_image")]
fn decode_test() {
let image = Cursor::new(VERIFICATION);
let image = image::load(image, ImageFormat::Png).unwrap().to_luma8();
let result = QrVerificationData::try_from(image).unwrap();
assert!(matches!(result, QrVerificationData::Verification(_)));
}
#[test]
#[cfg(feature = "decode_image")]
fn decode_encode_cycle() {
let image = Cursor::new(VERIFICATION);
let image = image::load(image, ImageFormat::Png).unwrap();
let result = QrVerificationData::from_image(image).unwrap();
assert!(matches!(result, QrVerificationData::Verification(_)));
let encoded = result.to_qr_code().unwrap();
let image = encoded.render::<Luma<u8>>().build();
let second_result = QrVerificationData::try_from(image).unwrap();
assert_eq!(result, second_result);
let bytes = result.to_bytes().unwrap();
let third_result = QrVerificationData::from_bytes(bytes).unwrap();
assert_eq!(result, third_result);
}
#[test]
#[cfg(feature = "decode_image")]
fn decode_encode_cycle_self() {
let image = Cursor::new(SELF_VERIFICATION);
let image = image::load(image, ImageFormat::Png).unwrap();
let result = QrVerificationData::try_from(image).unwrap();
assert!(matches!(result, QrVerificationData::SelfVerification(_)));
let encoded = result.to_qr_code().unwrap();
let image = encoded.render::<Luma<u8>>().build();
let second_result = QrVerificationData::from_luma(image).unwrap();
assert_eq!(result, second_result);
let bytes = result.to_bytes().unwrap();
let third_result = QrVerificationData::from_bytes(bytes).unwrap();
assert_eq!(result, third_result);
}
#[test]
#[cfg(feature = "decode_image")]
fn decode_encode_cycle_self_no_master() {
let image = Cursor::new(SELF_NO_MASTER);
let image = image::load(image, ImageFormat::Png).unwrap();
let result = QrVerificationData::from_image(image).unwrap();
assert!(matches!(result, QrVerificationData::SelfVerificationNoMasterKey(_)));
let encoded = result.to_qr_code().unwrap();
let image = encoded.render::<Luma<u8>>().build();
let second_result = QrVerificationData::try_from(image).unwrap();
assert_eq!(result, second_result);
let bytes = result.to_bytes().unwrap();
let third_result = QrVerificationData::try_from(bytes).unwrap();
assert_eq!(result, third_result);
}
#[test]
#[cfg(feature = "decode_image")]
fn decode_invalid_qr() {
let qr = QrCode::new(b"NonMatrixCode").expect("Can't build a simple QR code");
let image = qr.render::<Luma<u8>>().build();
let result = QrVerificationData::try_from(image);
assert!(matches!(result, Err(DecodingError::Header)))
}
#[test]
fn decode_invalid_header() {
let data = b"NonMatrixCode";

View File

@@ -15,14 +15,10 @@
use std::io::{Cursor, Read};
use byteorder::{BigEndian, ReadBytesExt};
#[cfg(feature = "decode_image")]
use image::{DynamicImage, GenericImage, GenericImageView, ImageBuffer, Luma};
use qrcode::QrCode;
use ruma_common::serde::Base64;
use vodozemac::Ed25519PublicKey;
#[cfg(feature = "decode_image")]
use crate::utils::decode_qr;
use crate::{
error::{DecodingError, EncodingError},
utils::{to_bytes, to_qr_code, HEADER, MAX_MODE, MIN_SECRET_LEN, VERSION},
@@ -41,25 +37,6 @@ pub enum QrVerificationData {
SelfVerificationNoMasterKey(SelfVerificationNoMasterKey),
}
#[cfg(feature = "decode_image")]
impl TryFrom<DynamicImage> for QrVerificationData {
type Error = DecodingError;
fn try_from(image: DynamicImage) -> Result<Self, Self::Error> {
Self::from_image(image)
}
}
// FIXME: We can't implement the generic trait because of https://github.com/rust-lang/rust/issues/50133
#[cfg(feature = "decode_image")]
impl TryFrom<ImageBuffer<Luma<u8>, Vec<u8>>> for QrVerificationData {
type Error = DecodingError;
fn try_from(image: ImageBuffer<Luma<u8>, Vec<u8>>) -> Result<Self, Self::Error> {
Self::from_luma(image)
}
}
impl TryFrom<&[u8]> for QrVerificationData {
type Error = DecodingError;
@@ -77,59 +54,6 @@ impl TryFrom<Vec<u8>> for QrVerificationData {
}
impl QrVerificationData {
/// Decode and parse an image of a QR code into a `QrVerificationData`
///
/// The image will be converted into a grey scale image before decoding is
/// attempted
///
/// # Arguments
///
/// * `image` - The image containing the QR code.
///
/// # Example
/// ```no_run
/// # use matrix_sdk_qrcode::{QrVerificationData, DecodingError};
/// # fn main() -> Result<(), DecodingError> {
/// use image;
///
/// let image = image::open("/path/to/my/image.png").unwrap();
/// let result = QrVerificationData::from_image(image)?;
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "decode_image")]
pub fn from_image(image: DynamicImage) -> Result<Self, DecodingError> {
let image = image.to_luma8();
Self::decode(image)
}
/// Decode and parse an grey scale image of a QR code into a
/// `QrVerificationData`
///
/// # Arguments
///
/// * `image` - The grey scale image containing the QR code.
///
/// # Example
/// ```no_run
/// # use matrix_sdk_qrcode::{QrVerificationData, DecodingError};
/// # fn main() -> Result<(), DecodingError> {
/// use image;
///
/// let image = image::open("/path/to/my/image.png").unwrap();
/// let image = image.to_luma8();
/// let result = QrVerificationData::from_luma(image)?;
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "decode_image")]
pub fn from_luma<I>(image: I) -> Result<Self, DecodingError>
where
I: GenericImage<Pixel = Luma<u8>> + GenericImageView<Pixel = Luma<u8>>,
{
Self::decode(image)
}
/// Parse the decoded payload of a QR code in byte slice form as a
/// `QrVerificationData`
///
@@ -292,17 +216,6 @@ impl QrVerificationData {
QrVerificationData::new(mode, flow_id, first_key, second_key, shared_secret)
}
/// Decode the given image of an QR code and if we find a valid code, try to
/// decode it as a `QrVerification`.
#[cfg(feature = "decode_image")]
fn decode<I>(image: I) -> Result<QrVerificationData, DecodingError>
where
I: GenericImage<Pixel = Luma<u8>> + GenericImageView<Pixel = Luma<u8>>,
{
let decoded = decode_qr(image)?;
Self::decode_bytes(decoded)
}
fn new(
mode: u8,
flow_id: Vec<u8>,

View File

@@ -12,14 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#[cfg(feature = "decode_image")]
use image::{GenericImage, GenericImageView, Luma};
use qrcode::{bits::Bits, EcLevel, QrCode, Version};
use ruma_common::serde::Base64;
use vodozemac::Ed25519PublicKey;
#[cfg(feature = "decode_image")]
use crate::error::DecodingError;
use crate::error::EncodingError;
pub(crate) const HEADER: &[u8] = b"MATRIX";
@@ -76,29 +72,3 @@ pub(crate) fn to_qr_code(
Ok(QrCode::with_bits(bits, EcLevel::L)?)
}
#[cfg(feature = "decode_image")]
pub(crate) fn decode_qr<I>(image: I) -> Result<Vec<u8>, DecodingError>
where
I: GenericImage<Pixel = Luma<u8>> + GenericImageView<Pixel = Luma<u8>>,
{
let mut image = rqrr::PreparedImage::prepare(image);
let grids = image.detect_grids();
let mut error = None;
for grid in grids {
let mut decoded = Vec::new();
match grid.decode_to(&mut decoded) {
Ok(_) => {
if decoded.starts_with(HEADER) {
return Ok(decoded);
}
}
Err(e) => error = Some(e),
}
}
Err(error.map(|e| e.into()).unwrap_or_else(|| DecodingError::Header))
}