固定宽高比
“矫正”根据照片估算文档的宽高比,估算不可能完全准确。当你的应用知道用户拍的是什么,例如 US Legal 表单或护照页时,aspectRatio 会让结果采用真实的形状。这个示例让用户在矫正之前选择文档类型。
html
<select id="type">
<option value="legal">US Legal 纸</option>
<option value="passport">护照页</option>
<option value="envelope">DL 信封</option>
<option value="other">其他</option>
</select>
<div id="viewer" style="height: 480px"></div>ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const proportions: Record<string, number | undefined> = {
legal: 8.5 / 14,
passport: 125 / 88,
envelope: 220 / 110,
other: undefined,
}
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
src: '/scans/form.jpg',
alt: '已签名的表单',
tools: ['perspective', 'rotate', 'finish'],
aspectRatio: proportions.legal,
})
document.querySelector('#type')!.addEventListener('change', (event) => {
const type = (event.target as HTMLSelectElement).value
pic.setOptions({ aspectRatio: proportions[type] })
})