Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Satini_pvduc
/
ocrpdf
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Authored by
tien_nemo
2025-08-13 10:36:58 +0700
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
26a1730a54de1a51fc8ba58fbeeaa481809bc26e
26a1730a
1 parent
63ba4a03
test load data
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
170 additions
and
70 deletions
app/Http/Controllers/OcrController.php
app/Http/Controllers/OcrController.php
View file @
26a1730
...
...
@@ -20,103 +20,203 @@ class OcrController extends Controller
$request
->
validate
([
'customer_name_text'
=>
'required|string'
,
'customer_name_xy'
=>
'required|string'
,
'template_name'
=>
'unique:mst_template,tpl_name'
,
'template_name'
=>
'required|string|unique:mst_template,tpl_name'
,
'fields'
=>
'required|array'
,
'fields.*.name'
=>
'required|string'
,
'fields.*.xy'
=>
'required|string'
,
]);
// Lưu vào bảng mst_template
$mst
=
MstTemplate
::
create
([
'tpl_name'
=>
$request
->
template_name
,
'tpl_text'
=>
$request
->
customer_name_text
,
'tpl_xy'
=>
$request
->
customer_name_xy
,
]);
try
{
// Lưu vào bảng mst_template
$mst
=
MstTemplate
::
create
([
'tpl_name'
=>
$request
->
template_name
,
'tpl_text'
=>
$request
->
customer_name_text
,
'tpl_xy'
=>
$request
->
customer_name_xy
,
]);
// Lưu các field khác vào dt_template
foreach
(
$request
->
fields
as
$field
)
{
DtTemplate
::
create
([
'tpl_id'
=>
$mst
->
id
,
'field_name'
=>
$field
[
'name'
],
'field_xy'
=>
$field
[
'xy'
],
// Lưu các field khác vào dt_template
foreach
(
$request
->
fields
as
$field
)
{
DtTemplate
::
create
([
'tpl_id'
=>
$mst
->
id
,
'field_name'
=>
$field
[
'name'
],
'field_xy'
=>
$field
[
'xy'
],
]);
}
return
response
()
->
json
([
'success'
=>
true
,
'message'
=>
'Lưu template thành công'
,
'template_id'
=>
$mst
->
id
]);
}
catch
(
\Exception
$e
)
{
return
response
()
->
json
([
'success'
=>
false
,
'message'
=>
'Lỗi khi lưu template: '
.
$e
->
getMessage
()
],
500
);
}
return
response
()
->
json
([
'success'
=>
true
,
'message'
=>
'Lưu template thành công'
]);
}
public
function
getData
()
public
function
getData
(
Request
$request
)
{
// Giả sử file OCR JSON & ảnh nằm trong storage/app/public/image/
$jsonPath
=
public_path
(
"image/data_picking_detail_1754967679.json"
);
$imgPath
=
(
"image/data_picking_detail_1754967679.jpg"
);
$templateName
=
'nemo'
;
/// Lấy từ request hoặc mặc định
if
(
!
file_exists
(
$jsonPath
))
{
return
response
()
->
json
([
'error'
=>
'File not found'
],
404
);
}
try
{
// Lấy template name từ request hoặc mặc định
$templateName
=
$request
->
get
(
'template_name'
,
'nemo'
);
$ocrData
=
json_decode
(
file_get_contents
(
$jsonPath
),
true
);
$formData
=
[];
// Giả sử file OCR JSON & ảnh nằm trong storage/app/public/image/
$jsonPath
=
public_path
(
"image/data_picking_detail_1754967679.json"
);
$imgPath
=
(
"image/data_picking_detail_1754967679.jpg"
);
if
(
$templateName
)
{
$mst
=
MstTemplate
::
where
(
'tpl_name'
,
$templateName
)
->
first
();
if
(
$mst
)
{
// Lấy detail của template
$details
=
DtTemplate
::
where
(
'tpl_id'
,
$mst
->
id
)
->
get
();
foreach
(
$details
as
$detail
)
{
$coords
=
array_map
(
'intval'
,
explode
(
','
,
$detail
->
field_xy
));
// coords = [x1, y1, x2, y2]
if
(
!
file_exists
(
$jsonPath
))
{
return
response
()
->
json
([
'error'
=>
'File OCR JSON không tìm thấy'
],
404
);
}
// Tìm text OCR nằm trong bbox này
$text
=
$this
->
findTextInBBox
(
$ocrData
,
$coords
);
$ocrData
=
json_decode
(
file_get_contents
(
$jsonPath
),
true
);
if
(
json_last_error
()
!==
JSON_ERROR_NONE
)
{
return
response
()
->
json
([
'error'
=>
'File OCR JSON không hợp lệ'
],
400
);
}
// field_name => text
$formData
[
$detail
->
field_name
]
=
$text
;
$formData
=
[];
if
(
$templateName
)
{
$mst
=
MstTemplate
::
where
(
'tpl_name'
,
$templateName
)
->
first
();
if
(
$mst
)
{
// Lấy detail của template
$details
=
DtTemplate
::
where
(
'tpl_id'
,
$mst
->
id
)
->
get
();
foreach
(
$details
as
$detail
)
{
$coords
=
array_map
(
'intval'
,
explode
(
','
,
$detail
->
field_xy
));
// coords = [x1, y1, x2, y2]
// Tìm text OCR nằm trong bbox này (cải thiện để gộp text)
$text
=
$this
->
findTextInBBox
(
$ocrData
,
$coords
);
// field_name => text
$formData
[
$detail
->
field_name
]
=
$text
;
}
}
else
{
$formData
=
[
'export_date'
=>
""
,
'order_code'
=>
""
,
'customer'
=>
""
,
'address'
=>
""
,
'staff'
=>
""
,
'customer_name'
=>
""
];
}
}
else
{
$formData
=
[
'export_date'
=>
""
,
'order_code'
=>
""
,
'customer'
=>
""
,
'address'
=>
""
,
'staff'
=>
""
,
'customer_name'
=>
""
];
}
return
response
()
->
json
([
'success'
=>
true
,
'ocrData'
=>
$ocrData
,
'pdfImageUrl'
=>
$imgPath
,
'formData'
=>
$formData
,
'fieldOptions'
=>
[
[
'value'
=>
'template_name'
,
'label'
=>
'Tên Mẫu PDF'
],
[
'value'
=>
'customer_name'
,
'label'
=>
'Tên khách hàng'
],
[
'value'
=>
'export_date'
,
'label'
=>
'Ngày xuất'
],
[
'value'
=>
'order_code'
,
'label'
=>
'Mã đơn hàng'
],
[
'value'
=>
'customer'
,
'label'
=>
'Khách hàng'
],
[
'value'
=>
'address'
,
'label'
=>
'Địa chỉ'
],
[
'value'
=>
'staff'
,
'label'
=>
'Nhân viên'
],
],
'template_name'
=>
$templateName
]);
}
catch
(
\Exception
$e
)
{
return
response
()
->
json
([
'success'
=>
false
,
'error'
=>
'Lỗi khi load data: '
.
$e
->
getMessage
()
],
500
);
}
return
response
()
->
json
([
'ocrData'
=>
$ocrData
,
'pdfImageUrl'
=>
$imgPath
,
'formData'
=>
$formData
,
'fieldOptions'
=>
[
[
'value'
=>
'template_name'
,
'label'
=>
'Tên Mẫu PDF'
],
[
'value'
=>
'customer_name'
,
'label'
=>
'Tên khách hàng'
],
[
'value'
=>
'export_date'
,
'label'
=>
'Ngày xuất'
],
[
'value'
=>
'order_code'
,
'label'
=>
'Mã đơn hàng'
],
[
'value'
=>
'customer'
,
'label'
=>
'Khách hàng'
],
[
'value'
=>
'address'
,
'label'
=>
'Địa chỉ'
],
[
'value'
=>
'staff'
,
'label'
=>
'Nhân viên'
],
]
]);
}
private
function
findTextInBBox
(
$ocrData
,
$coords
)
{
[
$x1
,
$y1
,
$x2
,
$y2
]
=
$coords
;
$foundItems
=
[];
foreach
(
$ocrData
as
$item
)
{
[
$ix1
,
$iy1
,
$ix2
,
$iy2
]
=
$item
[
'bbox'
];
// Kiểm tra nếu bbox OCR nằm trong vùng bbox template
// Kiểm tra xem box OCR có nằm trong vùng bbox template không
if
(
$ix1
>=
$x1
&&
$iy1
>=
$y1
&&
$ix2
<=
$x2
&&
$iy2
<=
$y2
)
{
return
$item
[
'text'
];
$foundItems
[]
=
[
'text'
=>
$item
[
'text'
],
'bbox'
=>
$item
[
'bbox'
],
'x'
=>
$ix1
,
'y'
=>
$iy1
];
}
}
if
(
empty
(
$foundItems
))
{
return
''
;
}
// Sắp xếp các item theo vị trí (từ trái sang phải, từ trên xuống dưới)
usort
(
$foundItems
,
function
(
$a
,
$b
)
{
// Ưu tiên theo Y trước (hàng), sau đó theo X (cột)
if
(
abs
(
$a
[
'y'
]
-
$b
[
'y'
])
<
20
)
{
// Cùng hàng (toler$foundItems = {array[2]} ance 20px)
return
$a
[
'x'
]
-
$b
[
'x'
];
// Sắp xếp theo X
}
return
$a
[
'y'
]
-
$b
[
'y'
];
// Sắp xếp theo Y
});
// Gộp text theo thứ tự đã sắp xếp
$combinedText
=
[];
foreach
(
$foundItems
as
$item
)
{
$combinedText
[]
=
trim
(
$item
[
'text'
]);
}
return
''
;
return
implode
(
' '
,
$combinedText
);
}
/**
* Lấy danh sách template
*/
public
function
getTemplateList
()
{
try
{
$templates
=
MstTemplate
::
select
(
'id'
,
'tpl_name'
,
'tpl_text'
,
'in_date'
)
->
orderBy
(
'created_at'
,
'desc'
)
->
get
();
return
response
()
->
json
([
'success'
=>
true
,
'templates'
=>
$templates
]);
}
catch
(
\Exception
$e
)
{
return
response
()
->
json
([
'success'
=>
false
,
'error'
=>
'Lỗi khi lấy danh sách template: '
.
$e
->
getMessage
()
],
500
);
}
}
/**
* Xóa template
*/
public
function
deleteTemplate
(
$id
)
{
try
{
$template
=
MstTemplate
::
findOrFail
(
$id
);
// Xóa các field detail trước
DtTemplate
::
where
(
'tpl_id'
,
$id
)
->
delete
();
// Xóa template chính
$template
->
delete
();
return
response
()
->
json
([
'success'
=>
true
,
'message'
=>
'Xóa template thành công'
]);
}
catch
(
\Exception
$e
)
{
return
response
()
->
json
([
'success'
=>
false
,
'error'
=>
'Lỗi khi xóa template: '
.
$e
->
getMessage
()
],
500
);
}
}
}
...
...
Please
register
or
sign in
to post a comment