From d843b3a14bf1520a0ceefa016a3b91ec956bcaa7 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sat, 21 Sep 2024 19:37:29 +0800 Subject: [PATCH] Try to reduce false positives in ad detection --- app/src/main/rust/src/img/qr_code.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/src/main/rust/src/img/qr_code.rs b/app/src/main/rust/src/img/qr_code.rs index 12753e3fb..e23c3b108 100644 --- a/app/src/main/rust/src/img/qr_code.rs +++ b/app/src/main/rust/src/img/qr_code.rs @@ -1,13 +1,13 @@ use crate::{jni_throwing, with_bitmap_content}; -use anyhow::Ok; use image::{ImageBuffer, Rgba}; use jni::objects::JClass; use jni::sys::{jboolean, jobject}; use jni::JNIEnv; use jni_fn::jni_fn; use rxing::common::HybridBinarizer; -use rxing::qrcode::detector::Detector; -use rxing::{BinaryBitmap, RGBLuminanceSource}; +use rxing::qrcode::detector::FinderPatternFinder; +use rxing::{BinaryBitmap, DecodingHintDictionary, Point, RGBLuminanceSource}; +use std::f32::consts::FRAC_1_SQRT_2; use std::ptr::slice_from_raw_parts; fn detect_image_ad(image: ImageBuffer, &[u8]>) -> bool { @@ -16,7 +16,19 @@ fn detect_image_ad(image: ImageBuffer, &[u8]>) -> bool { let pixels = unsafe { &*slice_from_raw_parts(buffer.as_ptr() as *const u32, buffer.len()) }; let source = RGBLuminanceSource::new_with_width_height_pixels(width, height, pixels); let image = BinaryBitmap::new(HybridBinarizer::new(source)); - Detector::new(image.get_black_matrix()).detect().is_ok() + match FinderPatternFinder::new(image.get_black_matrix()).find(&DecodingHintDictionary::new()) { + Ok(info) => { + // Check if the region is squarish + let top_left = Point::from(info.getTopLeft()); + let top_right = Point::from(info.getTopRight()); + let bottom_left = Point::from(info.getBottomLeft()); + let length = top_right.distance(top_left); + let epsilon = length * 0.05; + (bottom_left.distance(top_left) - length).abs() < epsilon + && (bottom_left.distance(top_right) * FRAC_1_SQRT_2 - length).abs() < epsilon + } + Err(_) => false, + } } #[no_mangle]