{"id":7713,"date":"2025-12-31T11:42:30","date_gmt":"2025-12-31T03:42:30","guid":{"rendered":"https:\/\/support.fuxinsoft.cn\/?p=7713"},"modified":"2026-01-05T15:18:51","modified_gmt":"2026-01-05T07:18:51","slug":"bitmap-%e7%b1%bb%e5%a6%82%e4%bd%95%e4%b8%8eopencv%e7%9a%84mat%e7%b1%bb%e4%ba%92%e8%bd%ac%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/support.fuxinsoft.cn\/?p=7713","title":{"rendered":"Bitmap \u7c7b\u5982\u4f55\u4e0e\u5176\u4ed6\u7684\u56fe\u50cf\u7c7b\u4e92\u8f6c\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5f53\u4e1a\u52a1\u573a\u666f\u9700\u8981\u5c06 PDF \u6587\u4ef6\u6e32\u67d3\u4e3a\u4f4d\u56fe\uff0c\u5e76\u57fa\u4e8e\u8be5\u4f4d\u56fe\u5f00\u5c55\u56fe\u50cf\u5904\u7406\uff08\u5982\u88c1\u526a\u3001\u6ee4\u6ce2\uff09\u6216\u56fe\u50cf\u8bc6\u522b\uff08\u5982 OCR\u3001\u76ee\u6807\u68c0\u6d4b\uff09\u7b49\u540e\u7eed\u64cd\u4f5c\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06 Foxit PDF SDK \u81ea\u5e26\u7684<code>Bitmap<\/code>\u7c7b\u578b\uff0c\u8f6c\u6362\u4e3a\u5e38\u7528\u56fe\u50cf\u7c7b\u5e93\u7684\u4f4d\u56fe\u683c\u5f0f\uff08\u4ee5\u4e3b\u6d41\u7684 OpenCV&nbsp;<code>Mat<\/code>\u7c7b\u4e3a\u4f8b\uff09\uff0c\u4ee5\u6b64\u964d\u4f4e\u8de8\u5e93\u5f00\u53d1\u7684\u9002\u914d\u6210\u672c\uff0c\u4fbf\u6377\u5bf9\u63a5\u540e\u7eed\u4e1a\u52a1\u6d41\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f Foxit PDF SDK\u00a0<code>Bitmap<\/code>\u00a0\u4e0e OpenCV\u00a0<code>Mat<\/code>\u00a0\u53cc\u5411\u4e92\u8f6c\u7684\u5b8c\u6574\u793a\u4f8b(java\u7248\u672c)\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n    private static Bitmap Mat2Bitmap(Mat mat) throws PDFException {\n        if (mat.empty()) {\n            throw new RuntimeException(\"Mat is empty\");\n        }\n\n        int width = mat.cols();\n        int height = mat.rows();\n        int channels = mat.channels();\n\n        Mat convertedMat = new Mat();\n        if (channels == 3) {\n            \/\/ OpenCV \u9ed8\u8ba4\u662f BGR\uff0c\u8f6c\u56de RGB\n            Imgproc.cvtColor(mat, convertedMat, Imgproc.COLOR_BGR2RGB);\n        } else if (channels == 4) {\n            \/\/ OpenCV \u662f BGRA\uff0c\u8f6c\u56de RGBA\n            Imgproc.cvtColor(mat, convertedMat, Imgproc.COLOR_BGRA2RGBA);\n        } else if (channels == 1) {\n            \/\/ \u7070\u5ea6\u56fe\u65e0\u9700\u8f6c\u6362\n            convertedMat = mat.clone();\n        } else {\n            \/\/\u4e0d\u652f\u6301\u7684\u901a\u9053\u6570\n            throw new RuntimeException(\"Unsupported channels: \" + channels);\n        }\n        int bpp = channels * 8; \/\/ \u6bcf\u4e2a\u50cf\u7d20\u7684\u4f4d\u6570\n        int rowValidBytes = width * channels; \/\/ \u6bcf\u884c\u6709\u6548\u50cf\u7d20\u5b57\u8282\u6570\uff08\u65e0\u586b\u5145\uff09\n        int pitch = rowValidBytes;\n\n        int bufferSize = pitch * height;\n        byte&#091;] buffer = new byte&#091;bufferSize];\n        for (int row = 0; row &lt; height; row++) {\n            byte&#091;] rowData = new byte&#091;rowValidBytes];\n            convertedMat.get(row, 0, rowData);\n\n            \/\/ \u590d\u5236\u5230\u7f13\u51b2\u533a\u7684\u5bf9\u5e94\u884c\uff08\u5305\u542b\u586b\u5145\u5b57\u8282\uff09\n            int bufferRowStart = row * pitch;\n            System.arraycopy(rowData, 0, buffer, bufferRowStart, rowValidBytes);\n        }\n       Bitmap bitmap = new Bitmap(width, height, e_DIBArgb, buffer,pitch);\/\/ pitch, buffer);\n        convertedMat.release();\n        return bitmap;\n    }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n    public static Mat bitmapToMat(Bitmap bitmap) throws PDFException {\n        int width = bitmap.getWidth();\n        int height = bitmap.getHeight();\n        int bpp = bitmap.getBpp();\n        int pitch = bitmap.getPitch();\n        byte&#091;] buffer = bitmap.getBuffer();\n\n        if (buffer == null) {\n            throw new RuntimeException(\"Bitmap buffer is null\");\n        }\n\n        int matType;\n        int channels;\n        if (bpp == 8) {\n            matType = CvType.CV_8UC1;\n            channels = 1;\n        } else if (bpp == 24) {\n            matType = CvType.CV_8UC3;\n            channels = 3;\n        } else if (bpp == 32) {\n            matType = CvType.CV_8UC4;\n            channels = 4;\n        } else {\n            throw new RuntimeException(\"Unsupported BPP: \" + bpp);\n        }\n\n        Mat mat = new Mat(height, width, matType);\n        int rowValidBytes = width * channels;\n\n        for (int row = 0; row &lt; height; row++) {\n            int bufferRowStart = row * pitch;\n            byte&#091;] rowData = new byte&#091;rowValidBytes];\n            System.arraycopy(buffer, bufferRowStart, rowData, 0, rowValidBytes);\n\n            mat.put(row, 0, rowData);\n        }\n\n        if (bpp == 24) {\n            Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2BGR);\n        } else if (bpp == 32) {\n            Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGBA2BGRA);\n        }\n\n        return mat.clone();\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4e1a\u52a1\u573a\u666f\u9700\u8981\u5c06 PDF \u6587\u4ef6\u6e32\u67d3\u4e3a\u4f4d\u56fe\uff0c\u5e76\u57fa\u4e8e\u8be5\u4f4d\u56fe\u5f00\u5c55\u56fe\u50cf\u5904\u7406\uff08\u5982\u88c1\u526a\u3001\u6ee4\u6ce2\uff09\u6216\u56fe\u50cf\u8bc6\u522b\uff08\u5982 OCR\u3001\u76ee\u6807 [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[425,1277,1276,209,619],"class_list":["post-7713","post","type-post","status-publish","format-standard","hentry","category-gsdk","tag-bitmap","tag-mat","tag-opencv","tag-209","tag-619"],"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","_links":{"self":[{"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/posts\/7713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7713"}],"version-history":[{"count":2,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/posts\/7713\/revisions"}],"predecessor-version":[{"id":7741,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=\/wp\/v2\/posts\/7713\/revisions\/7741"}],"wp:attachment":[{"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/support.fuxinsoft.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}