Try to reduce false positives in ad detection

This commit is contained in:
FooIbar committed 2024-09-21 20:05:01 +08:00
1 parent b9da0eda8b
commit d843b3a14b
1 file changed
+16 -4
+16 -4
View File
@@ -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<Rgba<u8>, &[u8]>) -> bool {
@@ -16,7 +16,19 @@ fn detect_image_ad(image: ImageBuffer<Rgba<u8>, &[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]