npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rn-opencv-doc-perspective-correction

v1.0.8

Published

A React Native library for document corner detection and perspective correction using react-native-fast-opencv

Readme

rn-opencv-doc-perspective-correction

Thư viện React Native chuyên biệt cho Page Corner DetectionPerspective Correction — chạy hoàn toàn offline (on-device) thông qua react-native-fast-opencv và OpenCV C++ JSI.

Tính năng

  • Page Corner Detection: Tự động nhận diện 4 góc của tờ giấy/tài liệu trong ảnh bất kỳ.
  • Perspective Correction: Bóp phẳng ảnh nghiêng dựa trên 4 góc nhận diện, xuất ra ảnh tài liệu chữ nhật hoàn hảo.

Cài đặt

Thư viện này là wrapper của react-native-fast-opencv. Đảm bảo peer dependency đã được cài trong dự án chính.

# Cài thư viện (local development)
npm install react-native-fast-opencv
npm install file:../rn-opencv-doc-perspective-correction

Hoặc thêm trực tiếp vào package.json:

{
  "dependencies": {
    "rn-opencv-doc-perspective-correction": "file:../rn-opencv-doc-perspective-correction",
    "react-native-fast-opencv": "^0.4.8"
  }
}

Cách sử dụng

Luồng hoàn chỉnh (Auto-detect + Warp)

import { DocumentScanner } from 'rn-opencv-doc-perspective-correction';
import RNFS from 'react-native-fs';

// 1. Đọc ảnh thành Base64
const imageBase64 = await RNFS.readFile('/path/to/image.jpg', 'base64');

// 2. Auto-detect 4 góc giấy
const corners = DocumentScanner.detectPageCorners(imageBase64);

if (corners) {
  console.log('Tìm thấy góc:', corners);
  // corners = [
  //   { x: 45,  y: 120 },  // Top-Left
  //   { x: 940, y: 100 },  // Top-Right
  //   { x: 960, y: 1280 }, // Bottom-Right
  //   { x: 30,  y: 1300 }, // Bottom-Left
  // ]

  // 3. Bóp phẳng ảnh theo 4 góc tìm được (hoặc góc sau khi user chỉnh tay)
  const scannedBase64 = DocumentScanner.applyPerspectiveCorrection(imageBase64, corners);

  if (scannedBase64) {
    // 4. Lưu lại file kết quả
    await RNFS.writeFile('/path/to/scanned.jpg', scannedBase64, 'base64');
  }
}

Kết hợp với UI tuỳ chỉnh (Bán tự động)

import { DocumentScanner, Point } from 'rn-opencv-doc-perspective-correction';

// i. Máy đoán góc trước
const autoCorners = DocumentScanner.detectPageCorners(imageBase64);

// ii. Hiển thị ảnh + 4 nút draggable tại vị trí autoCorners (ví dụ dùng rn-image-crop-skew)
//     Người dùng có thể kéo thả để tinh chỉnh

// iii. Sau khi người dùng ấn Done, lấy finalCorners từ UI component
const finalCorners: Point[] = getFinalCornersFromUI();

// iv. Warp với góc đã tinh chỉnh
const result = DocumentScanner.applyPerspectiveCorrection(imageBase64, finalCorners);

Thuật toán OpenCV bên dưới

Phase 1 — Detect Corners:

Input Image
  → cvtColor (BGR2GRAY)
  → GaussianBlur (5x5 kernel)
  → Canny Edge Detection (threshold: 75, 200)
  → findContours (RETR_LIST, CHAIN_APPROX_SIMPLE)
  → Lọc contour diện tích lớn nhất có đúng 4 đỉnh (approxPolyDP)
  → Sắp xếp 4 góc theo hướng kim đồng hồ (TL → TR → BR → BL)

Phase 2 — Perspective Correction:

[4 góc nguồn] + Input Image
  → Tính maxWidth & maxHeight bằng khoảng cách Euclidean
  → Tạo srcPoints (góc nghiêng gốc) và dstPoints (hình chữ nhật phẳng)
  → getPerspectiveTransform → Ma trận biến dạng
  → warpPerspective → Ảnh đã phẳng
  → toBase64 → Output

Lưu ý quan trọng

  • Hàm OpenCV.clearBuffers() được gọi tự động trong finally block của mỗi hàm để giải phóng bộ nhớ C++ JSI, tránh memory leak.
  • Kết quả detectPageCorners trả về undefined nếu không tìm thấy vật thể 4 góc rõ ràng — trong trường hợp đó, bạn nên yêu cầu User chỉnh tay.
  • Nên dùng ảnh độ phân giải vừa phải (1000-2000px) để thuật toán hoạt động chính xác nhất. Ảnh quá nhỏ sẽ mất chi tiết viền; ảnh quá lớn sẽ tốn RAM.