{"id":210,"date":"2020-06-12T17:00:44","date_gmt":"2020-06-12T09:00:44","guid":{"rendered":"http:\/\/8.154.33.202\/?p=210"},"modified":"2022-03-06T13:35:27","modified_gmt":"2022-03-06T05:35:27","slug":"%e5%9c%a8jni%e5%87%bd%e6%95%b0%e4%b8%ad%e4%bd%bf%e7%94%a8%e4%bc%a0%e5%85%a5%e7%9a%84bitmap","status":"publish","type":"post","link":"http:\/\/8.154.33.202\/?p=210","title":{"rendered":"\u5728jni\u51fd\u6570\u4e2d\u4f7f\u7528\u4f20\u5165\u7684bitmap"},"content":{"rendered":"\n<p>\u5728AS\u4e2d\uff0c\u6709\u65f6\u4f1a\u8981\u4f20\u9012bitmap\u7ed9c++\u51fd\u6570\uff0c\u4f7f\u7528opencv\u7b49\u5de5\u5177\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u65f6\u4f1a\u5c06bitmap\u4f20\u9012\u7ed9jni\u51fd\u6570\u3002bitmap\u5728jni\u7684\u51fd\u6570\u53c2\u6570\u4e2d\uff0c\u662fjobject\u7c7b\u578b\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u5c06\u4f20\u5165\u7684bitmap\u8f6c\u5316\u6210opencv\u5e38\u7528\u7684mat\u7c7b\u578b\uff0c\u4ece\u800c\u8fdb\u884c\u5904\u7406<\/p>\n\n\n\n<p>\u9996\u5148\u8981\u5305\u542bbitmap\u7684\u5934\u6587\u4ef6&lt;android\/bitmap.h&gt;\u3002\u8fd9\u4e2a\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e9b\u91cd\u8981\u4fe1\u606f\u3002\u4e86\u89e3\u8fd9\u4e9b\u4fe1\u606f\u975e\u5e38\u5fc5\u8981<\/p>\n\n\n\n<p><strong>AndroidBitmapInfo<\/strong> \u4e00\u4e2a\u5305\u542bbitmap\u5e38\u7528\u4fe1\u606f\u7684\u7ed3\u6784\u4f53<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/** Bitmap info, see AndroidBitmap_getInfo(). *\/<br><\/em><strong>typedef struct <\/strong>{<br>    <em>\/** The bitmap width in pixels. *\/<br><\/em><em>    <\/em>uint32_t    width;<br>    <em>\/** The bitmap height in pixels. *\/<br><\/em><em>    <\/em>uint32_t    height;<br>    <em>\/** The number of byte per row. *\/<br><\/em><em>    <\/em>uint32_t    stride;<br>    <em>\/** The bitmap pixel format. See {@link AndroidBitmapFormat} *\/<br><\/em><em>    <\/em>int32_t     format;<br>    <em>\/** Unused. *\/<br><\/em><em>    <\/em>uint32_t    flags;      <em>\/\/ 0 for now<br><\/em>} AndroidBitmapInfo;<\/pre>\n\n\n\n<p><strong>AndroidBitmapFormat<\/strong> \u56fe\u7247\u683c\u5f0f\u7684\u679a\u4e3e\u7c7b\u578b\u3002\u683c\u5f0f\u4e3a\u56fe\u7247\u7684\u50cf\u7d20\u4f4d\u6570\u3002\u6700\u5e38\u89c1\u7684\u662fANDROID_BITMAP_FORMAT_RGBA_8888<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/** Bitmap pixel format. *\/<br><\/em><strong>enum <\/strong>AndroidBitmapFormat {<br>    <em>\/** No format. *\/<br><\/em><em>    <\/em>ANDROID_BITMAP_FORMAT_NONE      = 0,<br>    <em>\/** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **\/<br><\/em><em>    <\/em>ANDROID_BITMAP_FORMAT_RGBA_8888 = 1,<br>    <em>\/** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **\/<br><\/em><em>    <\/em>ANDROID_BITMAP_FORMAT_RGB_565   = 4,<br>    <em>\/** Deprecated in API level 13. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead. **\/<br><\/em><em>    <\/em>ANDROID_BITMAP_FORMAT_RGBA_4444 = 7,<br>    <em>\/** Alpha: 8 bits. *\/<br><\/em><em>    <\/em>ANDROID_BITMAP_FORMAT_A_8       = 8,<br>};<\/pre>\n\n\n\n<p><strong>AndroidBitmap_getInfo<\/strong> \u83b7\u53d6bitmap\u4fe1\u606f\u7684\u51fd\u6570\u3002\u8fd4\u56de\u503c\u4e3a\u540e\u9762\u7684\u679a\u4e3e\u7c7b\u578b\u3002\u6210\u529f\u8bfb\u53d6\u7684\u8fd4\u56de\u503c\u4e3a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**<br><\/em><em> * Given a java bitmap object, fill out the AndroidBitmapInfo struct for it.<br><\/em><em> * If the call fails, the info parameter will be ignored.<br><\/em><em> *\/<br><\/em><strong>int <\/strong>AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap,<br>                          AndroidBitmapInfo* info);<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/** AndroidBitmap functions result code. *\/<br><\/em><strong>enum <\/strong>{<br>    <em>\/** Operation was successful. *\/<br><\/em><em>    <\/em>ANDROID_BITMAP_RESULT_SUCCESS           = 0,<br>    <em>\/** Bad parameter. *\/<br><\/em><em>    <\/em>ANDROID_BITMAP_RESULT_BAD_PARAMETER     = -1,<br>    <em>\/** JNI exception occured. *\/<br><\/em><em>    <\/em>ANDROID_BITMAP_RESULT_JNI_EXCEPTION     = -2,<br>    <em>\/** Allocation failed. *\/<br><\/em><em>    <\/em>ANDROID_BITMAP_RESULT_ALLOCATION_FAILED = -3,<br>};<\/pre>\n\n\n\n<p><strong>AndroidBitmap_lockPixels<\/strong>  \u975e\u5e38\u5173\u952e\u7684\u4e00\u4e2a\u51fd\u6570\u3002\u5c06bitmap\u7684\u50cf\u7d20\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u8fdb\u884c\u9501\u5b9a\uff0c\u786e\u4fdd\u8fd9\u4e00\u5757\u5185\u5b58\u4e0d\u4f1a\u88ab\u79fb\u52a8\u3002\u4f20\u5165\u7684addrPtr\u662f\u4e00\u4e2a\u4e8c\u7ea7\u6307\u9488\uff0c*addPtr\u662f\u6307\u5411bitmap\u50cf\u7d20\u5730\u5740\u7684\u6307\u9488\u3002\u8be5\u51fd\u6570\u6b63\u786e\u8c03\u7528\u540e\uff0c\u4e00\u5b9a\u8981\u5728\u8be5\u50cf\u7d20\u5730\u5740\u6307\u9488\u4e0d\u518d\u4f7f\u7528\u540e\uff0c\u4f7f\u7528<strong>AndroidBitmap_unlockPixels<\/strong>\u8fdb\u884c\u91ca\u653e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**<br><\/em><em> * Given a java bitmap object, attempt to lock the pixel address.<br><\/em><em> * Locking will ensure that the memory for the pixels will not move<br><\/em><em> * until the unlockPixels call, and ensure that, if the pixels had been<br><\/em><em> * previously purged, they will have been restored.<br><\/em><em> *<br><\/em><em> * If this call succeeds, it must be balanced by a call to<br><\/em><em> * AndroidBitmap_unlockPixels, after which time the address of the pixels should<br><\/em><em> * no longer be used.<br><\/em><em> *<br><\/em><em> * If this succeeds, *addrPtr will be set to the pixel address. If the call<br><\/em><em> * fails, addrPtr will be ignored.<br><\/em><em> *\/<br><\/em><strong>int <\/strong>AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, <strong>void<\/strong>** addrPtr);<\/pre>\n\n\n\n<p><strong>AndroidBitmap_unlockPixels<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**<br><\/em><em> * Call this to balance a successful call to AndroidBitmap_lockPixels.<br><\/em><em> *\/<br><\/em><strong>int <\/strong>AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap);<\/pre>\n\n\n\n<p>\u4e86\u89e3\u4ee5\u4e0a\u4fe1\u606f\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u63a2\u7d22\u5982\u4f55\u5c06\u4f20\u5165\u7684bitmap\u8f6c\u4e3a\u9700\u8981\u7684mat\u4e86\u3002\u901a\u7528\u7684\u505a\u6cd5\u662f\u5148\u83b7\u53d6bitmap\u7684\u4fe1\u606f<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">AndroidBitmapInfo img_info;\n<strong>CV_Assert<\/strong>(AndroidBitmap_getInfo(env, bitmap, &amp;img_info) >= 0);\n<strong>CV_Assert<\/strong>(img_info.format == <strong><em>ANDROID_BITMAP_FORMAT_RGBA_8888<\/em><\/strong>);<\/pre>\n\n\n\n<p>\u4f7f\u7528CV_Assert\u786e\u4fdd\u83b7\u53d6\u4fe1\u606f\u6210\u529f<\/p>\n\n\n\n<p>\u7136\u540e\u521d\u59cb\u5316\u4e00\u4e2a\u6307\u9488\uff0c\u5e76\u5c06\u8be5\u6307\u9488\u7684\u5730\u5740\u4f20\u9012\u7ed9AndroidBitmap_lockPixels\uff0c\u82e5\u6210\u529f\u5219\u8be5\u6307\u9488\u5373\u6307\u5411bitmap\u7684\u50cf\u7d20\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>void <\/strong>*ori_img_pxl = NULL;\n<strong>CV_Assert<\/strong>(AndroidBitmap_lockPixels(env, bitmap, &amp;ori_img_pxl) >= 0);\n<strong>CV_Assert<\/strong>(ori_img_pxl);<\/pre>\n\n\n\n<p>\u7136\u540e\u5c31\u53ef\u4ee5\u5c06\u8be5\u6307\u9488\u4f20\u9012\u7ed9mat\u7684\u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u51fa\u6211\u4eec\u9700\u8981\u7684mat\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Mat ori_img_mat(img_info.height, img_info.width, <strong>CV_8UC4<\/strong>, ori_img_pxl);<\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u5f97\u5230\u7684mat\u7684\u6570\u636e\u6307\u9488\u662f\u6307\u5411bitmap\u7684\u50cf\u7d20\u5185\u5b58\u7684\uff0c\u56e0\u6b64mat\u4e2d\u5bf9\u50cf\u7d20\u7684\u64cd\u4f5c\u4f1a\u76f4\u63a5\u53cd\u6620\u5728bitmap\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">AndroidBitmap_unlockPixels(env, bitmap);<\/pre>\n\n\n\n<p>\u6700\u540e\u8981\u8bb0\u5f97\u91ca\u653e\u9501\u5b9a\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728AS\u4e2d\uff0c\u6709\u65f6\u4f1a\u8981\u4f20\u9012bitmap\u7ed9c++\u51fd\u6570\uff0c\u4f7f\u7528opencv\u7b49\u5de5\u5177\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u65f6\u4f1a\u5c06bitmap\u4f20\u9012\u7ed9jn &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/8.154.33.202\/?p=210\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5728jni\u51fd\u6570\u4e2d\u4f7f\u7528\u4f20\u5165\u7684bitmap\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,10],"tags":[],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-android","category-opencv"],"_links":{"self":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/210"}],"collection":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210"}],"version-history":[{"count":4,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":308,"href":"http:\/\/8.154.33.202\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions\/308"}],"wp:attachment":[{"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/8.154.33.202\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}