@@ -104,6 +104,7 @@ let init = (async () => {
104104} ) ( ) ;
105105
106106export const handleCapturedResult = ( result , uploadedImage = null ) => {
107+ console . log ( result ) ;
107108 const recognizedResults = result . textLineResultItems ;
108109 const parsedResults = result . parsedResultItems ;
109110 const originalImage = result . items ?. [ 0 ] ?. imageData ;
@@ -125,7 +126,7 @@ export const handleCapturedResult = (result, uploadedImage = null) => {
125126 alert ( `Failed to parse the content.` ) ;
126127 parsedResultArea . style . justifyContent = "flex-start" ;
127128 }
128- displayImage ( uploadedImage || originalImage ) ;
129+ displayImage ( uploadedImage || originalImage , recognizedResults [ 0 ] . location . points ) ;
129130
130131 dispose ( ) ;
131132 } else if ( uploadedImage ) {
@@ -155,7 +156,7 @@ const displayResults = (recognizedText, parsedResult) => {
155156 return false ;
156157} ;
157158
158- function displayImage ( image ) {
159+ function displayImage ( image , points ) {
159160 scannedImage . textContent = "" ;
160161
161162 if ( image . type ?. startsWith ( "image/" ) ) {
@@ -164,9 +165,36 @@ function displayImage(image) {
164165
165166 img . src = imageUrl ;
166167 img . className = "uploaded-image" ;
167- img . onload = ( ) => URL . revokeObjectURL ( imageUrl ) ;
168-
169- scannedImage . append ( img ) ;
168+ img . onload = ( ) => {
169+ URL . revokeObjectURL ( imageUrl ) ;
170+
171+ const canvas = document . createElement ( "canvas" ) ;
172+ const ctx = canvas . getContext ( "2d" ) ;
173+
174+ const width = points [ 1 ] . x - points [ 0 ] . x ;
175+ const height = points [ 2 ] . y - points [ 1 ] . y ;
176+
177+ canvas . width = width ;
178+ canvas . height = height ;
179+
180+ ctx . drawImage (
181+ img ,
182+ points [ 0 ] . x ,
183+ points [ 0 ] . y ,
184+ width ,
185+ height , // Source coordinates
186+ 0 ,
187+ 0 ,
188+ width ,
189+ height // Destination coordinates
190+ ) ;
191+
192+ const croppedImage = new Image ( ) ;
193+ croppedImage . src = canvas . toDataURL ( ) ;
194+ croppedImage . className = "uploaded-image" ;
195+
196+ scannedImage . append ( croppedImage ) ;
197+ } ;
170198 } else if ( image . toCanvas ) {
171199 scannedImage . append ( image . toCanvas ( ) ) ;
172200 }
@@ -176,7 +204,6 @@ function dispose() {
176204 resultContainer . style . display = "flex" ; // Show result container
177205 cameraListContainer . style . display = "none" ; // hide header menu windows
178206 informationListContainer . style . display = "none" ;
179- uploadMenuList . style . display = "none" ;
180207 scanModeContainer . style . display = "none" ; // hide scan mode buttons
181208
182209 cameraEnhancer . close ( ) ;
0 commit comments