图像和扫描的文档可能包含您可能需要进一步处理的文本信息。可能已经使用智能手机拍摄了文本文档的图片,并将其转换为可编辑的文档。为此,对图像执行 OCR 可以证明是有帮助的。使用 OCR,可以将图像转换为可搜索和可编辑的 Word 文档。为此,本文将教您 如何使用 C++ 将图像转换为 Word 文档。
- 使用 C++ 将图像转换为 Word 文档
- 使用 C++ 将倾斜图像转换为 Word 文档
Aspose.OCR 是一种光学字符识别 API,可以从图像中提取文本。该 API 还使您能够将图像转换为 Word 文档和文本文件。如果你还没有用过Aspose.OCR可以下载最新版测试。
![1分钟学会在C++中将图像转换为Word文档(DOCX),快速入手光学字符处理控件Aspose.OCR!](https://img-blog.csdnimg.cn/img_convert/3457e647f269b37cf2dbba403eda19bb.png)
使用 C++ 将图像转换为 Word 文档
通常您可能需要搜索或编辑图像中的文本,但您无法这样做。通过对此类图像执行 OCR 并将其转换为 Word 文档,您可以根据需要搜索和编辑文本。为此,请按照以下步骤操作。
- 为结果准备缓冲区。
- 将RecognitionSettings.save_format结构成员的值设置为file_format::docx。
- 对图片进行OCR操作,使用asposeocr_page_save(const char * image_path, const char * save_path, RecognitionSettings settings)方法保存Word文件。
以下示例代码显示了如何使用 C++ 将图像转换为 Word 文档:
// Source file path
std::string image_path = "SourceDirectory\\sample.png";
// Prepare buffer for result (in symbols, len_byte = len * sizeof(wchar_t))
const size_t len = 4096;
wchar_t buffer[len] = { 0 };
// Set the recognition settings
RecognitionSettings settings;
settings.save_format = file_format::docx;
// Perform the OCR operation and save the output file.
asposeocr_page_save(image_path.c_str(), "OutputDirectory\\ImageToDocx.Docx", settings);
![1分钟学会在C++中将图像转换为Word文档(DOCX),快速入手光学字符处理控件Aspose.OCR!](https://img-blog.csdnimg.cn/img_convert/b40f8d84a09e6069f7c65b20aa05aca3.png)
使用 C++ 将倾斜图像转换为 Word 文档
图像和扫描的文档有时会倾斜。使用 Aspose.OCR for C++ API,您可以对倾斜的图像执行 OCR。以下是将倾斜图像转换为 Word 文档的步骤:
- 为结果准备缓冲区。
- 使用asposeocr_get_skew(const char * image_path) 方法计算图像的倾斜角度 。
- 将RecognitionSettings.save_format 结构成员的值设置 为 file_format::docx。
- 使用RecognitionSettings.skew 结构成员指定倾斜角度 。
- 对图片进行OCR操作,使用asposeocr_page_save(const char * image_path, const char * save_path, RecognitionSettings settings) 方法保存Word文件 。
以下示例代码显示了如何使用 C++ 将倾斜图像转换为 Word 文档:
// Source file path
std::string image_path = "SourceDirectory\\skewSample.png";
// Prepare buffer for result (in symbols, len_byte = len * sizeof(wchar_t))
const size_t len = 4096;
wchar_t buffer[len] = { 0 };
// Calculate skew angle
std::double_t angle = asposeocr_get_skew(image_path.c_str());
// Set the recognition settings
RecognitionSettings settings;
settings.save_format = file_format::docx;
settings.skew = angle;
// Perform the OCR operation and save the output file.
asposeocr_page_save(image_path.c_str(), "OutputDirectory\\SkewedImageToDocx.docx", settings);
![1分钟学会在C++中将图像转换为Word文档(DOCX),快速入手光学字符处理控件Aspose.OCR!](https://img-blog.csdnimg.cn/img_convert/1913758cea2af3fe6dcd959f5328c519.png)
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。