11#!/usr/bin/env node
22
33import { createDetector , TAG_FAMILIES } from '../dist/index.js' ;
4- import { Jimp } from 'jimp ' ;
4+ import sharp from 'sharp ' ;
55import { createServer } from 'http' ;
66import { fileURLToPath } from 'url' ;
77import { dirname , join , basename } from 'path' ;
@@ -14,17 +14,18 @@ const PORT = 3000;
1414async function detectAndServe ( imagePath , blackBorder = 1 ) {
1515 console . log ( `\nLoading image: ${ imagePath } ` ) ;
1616
17- const image = await Jimp . read ( imagePath ) ;
18- const width = image . bitmap . width ;
19- const height = image . bitmap . height ;
17+ const image = sharp ( imagePath ) ;
18+ const metadata = await image . metadata ( ) ;
19+ const width = metadata . width ;
20+ const height = metadata . height ;
2021
2122 console . log ( `Image dimensions: ${ width } x${ height } ` ) ;
2223
2324 // Convert to grayscale for detection
24- const grayImage = image . clone ( ) . greyscale ( ) ;
25- const buffer = Buffer . from (
26- grayImage . bitmap . data . filter ( ( _ , i ) => i % 4 === 0 )
27- ) ;
25+ const { data : buffer } = await image
26+ . greyscale ( )
27+ . raw ( )
28+ . toBuffer ( { resolveWithObject : true } ) ;
2829
2930 console . log ( `Creating detector with blackBorder=${ blackBorder } ...` ) ;
3031 const detector = createDetector ( TAG_FAMILIES . TAG_36H11 , { blackBorder } ) ;
@@ -42,7 +43,9 @@ async function detectAndServe(imagePath, blackBorder = 1) {
4243 } ) ;
4344
4445 // Convert image to base64 for embedding
45- const imageBase64 = await image . getBase64 ( 'image/jpeg' ) ;
46+ const imageBase64 =
47+ 'data:image/jpeg;base64,' +
48+ ( await sharp ( imagePath ) . jpeg ( ) . toBuffer ( ) ) . toString ( 'base64' ) ;
4649
4750 const htmlPage = generateHTML (
4851 imageBase64 ,
0 commit comments