Skip to content

Commit ab4ba13

Browse files
committed
Update
1 parent ef11f13 commit ab4ba13

File tree

5 files changed

+163
-124
lines changed

5 files changed

+163
-124
lines changed

docscanner/__init__.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from .docscanner import *
22
import os
33
import json
4+
import numpy as np
5+
46
__version__ = version
57

68
class ImagePixelFormat:
@@ -31,3 +33,95 @@ class ImagePixelFormat:
3133
# 24bit with BGR channel order stored in memory from high to low address
3234
IPF_BGR_888 = 12
3335

36+
def convertNormalizedImage2Mat(normalized_image):
37+
bytearray = normalized_image.bytearray
38+
width = normalized_image.width
39+
height = normalized_image.height
40+
41+
channels = 3
42+
if normalized_image.format == ImagePixelFormat.IPF_BINARY:
43+
channels = 1
44+
all = []
45+
46+
for byte in bytearray:
47+
48+
byteCount = 7
49+
while byteCount >= 0:
50+
b = (byte & (1 << byteCount)) >> byteCount
51+
if b == 1:
52+
all.append(255)
53+
else:
54+
all.append(0)
55+
56+
byteCount -= 1
57+
58+
bytearray = all
59+
width = normalized_image.stride * 8
60+
61+
elif normalized_image.format == ImagePixelFormat.IPF_GRAYSCALED:
62+
channels = 1
63+
64+
mat = np.array(bytearray, dtype=np.uint8).reshape(height, width, channels)
65+
66+
return mat
67+
68+
class Templates:
69+
binary = '''
70+
{
71+
"GlobalParameter":{
72+
"Name":"GP"
73+
},
74+
"ImageParameterArray":[
75+
{
76+
"Name":"IP-1",
77+
"NormalizerParameterName":"NP-1"
78+
}
79+
],
80+
"NormalizerParameterArray":[
81+
{
82+
"Name":"NP-1",
83+
"ColourMode": "ICM_BINARY"
84+
}
85+
]
86+
}
87+
'''
88+
89+
color = '''
90+
{
91+
"GlobalParameter":{
92+
"Name":"GP"
93+
},
94+
"ImageParameterArray":[
95+
{
96+
"Name":"IP-1",
97+
"NormalizerParameterName":"NP-1"
98+
}
99+
],
100+
"NormalizerParameterArray":[
101+
{
102+
"Name":"NP-1",
103+
"ColourMode": "ICM_COLOUR"
104+
}
105+
]
106+
}
107+
'''
108+
109+
grayscale = '''
110+
{
111+
"GlobalParameter":{
112+
"Name":"GP"
113+
},
114+
"ImageParameterArray":[
115+
{
116+
"Name":"IP-1",
117+
"NormalizerParameterName":"NP-1"
118+
}
119+
],
120+
"NormalizerParameterArray":[
121+
{
122+
"Name":"NP-1",
123+
"ColourMode": "ICM_GRAYSCALE"
124+
}
125+
]
126+
}
127+
'''

src/docscanner.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,4 @@ PyMODINIT_FUNC PyInit_docscanner(void)
101101
PyModule_AddStringConstant(module, "version", DDN_GetVersion());
102102
return module;
103103
}
104+

src/document_scanner.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void scan(DynamsoftDocumentScanner *self, unsigned char *buffer, int width, int
253253
int ret = DDN_DetectQuadFromBuffer(self->handler, &data, "", &pResults);
254254
if (ret)
255255
{
256-
printf("Detection error: %s\n", DC_GetErrorString(ret));
256+
// printf("Detection error: %s\n", DC_GetErrorString(ret));
257257
}
258258

259259
free(buffer);
@@ -400,15 +400,22 @@ static PyObject *setParameters(PyObject *obj, PyObject *args)
400400
return Py_BuildValue("i", ret);
401401
}
402402

403-
PyObject *createNormalizedImage(ImageData *imageData)
403+
PyObject *createNormalizedImage(NormalizedImageResult* normalizedResult)
404404
{
405+
if (normalizedResult == NULL)
406+
{
407+
return NULL;
408+
}
409+
410+
ImageData *imageData = normalizedResult->image;
405411
NormalizedImage *ni = PyObject_New(NormalizedImage, &NormalizedImageType);
406412
ni->bytearray = PyByteArray_FromStringAndSize((const char *)imageData->bytes, imageData->bytesLength);
407413
ni->length = Py_BuildValue("i", imageData->bytesLength);
408414
ni->width = Py_BuildValue("i", imageData->width);
409415
ni->height = Py_BuildValue("i", imageData->height);
410416
ni->stride = Py_BuildValue("i", imageData->stride);
411417
ni->format = Py_BuildValue("i", imageData->format);
418+
ni->normalizedResult = normalizedResult;
412419
return (PyObject *)ni;
413420
}
414421
/**
@@ -442,12 +449,7 @@ static PyObject *normalizeFile(PyObject *obj, PyObject *args)
442449
if (errorCode != DM_OK)
443450
printf("%s\r\n", DC_GetErrorString(errorCode));
444451

445-
ImageData *imageData = normalizedResult->image;
446-
447-
PyObject *normalizedImage = createNormalizedImage(imageData);
448-
449-
if (normalizedResult != NULL)
450-
DDN_FreeNormalizedImageResult(&normalizedResult);
452+
PyObject *normalizedImage = createNormalizedImage(normalizedResult);
451453

452454
return normalizedImage;
453455
}
@@ -523,12 +525,7 @@ static PyObject *normalizeBuffer(PyObject *obj, PyObject *args)
523525
if (errorCode != DM_OK)
524526
printf("%s\r\n", DC_GetErrorString(errorCode));
525527

526-
ImageData *imageData = normalizedResult->image;
527-
528-
PyObject *normalizedImage = createNormalizedImage(imageData);
529-
530-
if (normalizedResult != NULL)
531-
DDN_FreeNormalizedImageResult(&normalizedResult);
528+
PyObject *normalizedImage = createNormalizedImage(normalizedResult);
532529

533530
Py_DECREF(memoryview);
534531

src/normalized_image.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct
1414
PyObject *height;
1515
PyObject *stride;
1616
PyObject *format;
17+
NormalizedImageResult* normalizedResult;
1718
} NormalizedImage;
1819

1920
static void NormalizedImage_dealloc(NormalizedImage *self)
@@ -24,6 +25,7 @@ static void NormalizedImage_dealloc(NormalizedImage *self)
2425
if (self->height) Py_DECREF(self->height);
2526
if (self->stride) Py_DECREF(self->stride);
2627
if (self->format) Py_DECREF(self->format);
28+
if (self->normalizedResult) DDN_FreeNormalizedImageResult(&self->normalizedResult);
2729
Py_TYPE(self)->tp_free((PyObject *)self);
2830
}
2931

@@ -32,9 +34,30 @@ static PyObject *NormalizedImage_new(PyTypeObject *type, PyObject *args, PyObjec
3234
NormalizedImage *self;
3335

3436
self = (NormalizedImage *)type->tp_alloc(type, 0);
37+
self->normalizedResult = NULL;
3538
return (PyObject *)self;
3639
}
3740

41+
static PyObject *save(PyObject *obj, PyObject *args)
42+
{
43+
NormalizedImage *self = (NormalizedImage *)obj;
44+
45+
char *pFileName; // File name
46+
if (!PyArg_ParseTuple(args, "s", &pFileName))
47+
{
48+
return NULL;
49+
}
50+
51+
if (self->normalizedResult)
52+
{
53+
DDN_SaveImageDataToFile(self->normalizedResult->image, pFileName);
54+
printf("Save image to file: %s\n", pFileName);
55+
return Py_BuildValue("i", 0);
56+
}
57+
58+
return Py_BuildValue("i", -1);
59+
}
60+
3861
static PyMemberDef NormalizedImage_members[] = {
3962
{"bytearray", T_OBJECT_EX, offsetof(NormalizedImage, bytearray), 0, "bytearray"},
4063
{"length", T_OBJECT_EX, offsetof(NormalizedImage, length), 0, "length"},
@@ -45,6 +68,11 @@ static PyMemberDef NormalizedImage_members[] = {
4568
{NULL} /* Sentinel */
4669
};
4770

71+
static PyMethodDef ni_instance_methods[] = {
72+
{"save", save, METH_VARARGS, NULL},
73+
{NULL, NULL, 0, NULL}
74+
};
75+
4876
static PyTypeObject NormalizedImageType = {
4977
PyVarObject_HEAD_INIT(NULL, 0) "docscanner.NormalizedImage", /* tp_name */
5078
sizeof(NormalizedImage), /* tp_basicsize */
@@ -72,7 +100,7 @@ static PyTypeObject NormalizedImageType = {
72100
0, /* tp_weaklistoffset */
73101
0, /* tp_iter */
74102
0, /* tp_iternext */
75-
0, /* tp_methods */
103+
ni_instance_methods, /* tp_methods */
76104
NormalizedImage_members, /* tp_members */
77105
0, /* tp_getset */
78106
0, /* tp_base */

0 commit comments

Comments
 (0)