{"id":1912,"date":"2022-11-25T18:00:43","date_gmt":"2022-11-25T10:00:43","guid":{"rendered":"https:\/\/app.applebyme.cn\/cloud\/appshangjia\/1912.html"},"modified":"2022-11-25T18:00:43","modified_gmt":"2022-11-25T10:00:43","slug":"%e5%b0%8f%e7%b1%b3%e5%bc%80%e5%8f%91%e5%b9%b3%e5%8f%b0%e5%ba%94%e7%94%a8%e8%b0%83%e7%94%a8camera%e9%a2%84%e8%a7%88%e6%95%88%e6%9e%9c%e9%80%82%e9%85%8d%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/app.applebyme.cn\/cloud\/appshangjia\/1912.html","title":{"rendered":"\u5c0f\u7c73\u5f00\u53d1\u5e73\u53f0\u5e94\u7528\u8c03\u7528Camera\u9884\u89c8\u6548\u679c\u9002\u914d\u8bf4\u660e"},"content":{"rendered":"<p><span class=\"wpcom_tag_link\">\u5c0f\u7c73\u5f00\u53d1\u5e73\u53f0<\/span>\u5e94\u7528<span class=\"wpcom_tag_link\">\u8c03\u7528Camera<\/span>\u9884\u89c8\u6548\u679c\u9002\u914d\u8bf4\u660e<\/p>\n<h2 id=\"_0\">1.\u5e94\u7528\u4e2d\u62cd\u6444\u6216\u626b\u4e00\u626b\u529f\u80fd\u9884\u89c8\u9875\u9762\u51fa\u73b0\u62c9\u4f38\u73b0\u8c61<\/h2>\n<p>\u7531\u4e8e\u4e0d\u540c\u624b\u673a\u5c4f\u5e55\u5c3a\u5bf8\u6bd4\u4f8b\u4e0d\u5c3d\u76f8\u540c\uff0c\u6216\u8bb8\u5e94\u7528\u5728\u8c03\u7528<span class=\"wpcom_tag_link\">Camera\u9884\u89c8<\/span>\u5b9e\u73b0\u65b9\u6cd5\u4e0d\u6807\u51c6\u6216\u7b2c\u4e09\u65b9SDK\u7ef4\u62a4\u4e0d\u53ca\u65f6\uff0c\u63d0\u4f9b\u7684\u9884\u89c8\u7a97\u53e3\u5bbd\u9ad8\u6bd4\u4f8b\u548c<span class=\"wpcom_tag_link\">Camera<\/span>\u8f93\u51fa\u56fe\u50cf\u7684\u5bbd\u9ad8\u6bd4\u4f8b\u4e0d\u4e00\u81f4\uff0c\u4ee5\u81f3\u4e8e\u5e94\u7528\u5728\u4f7f\u7528\u62cd\u6444\u6216\u626b\u4e00\u626b\u529f\u80fd\u65f6\u9884\u89c8\u9875\u9762\u5c31\u53ef\u80fd\u51fa\u73b0\u62c9\u4f38\u73b0\u8c61\u3002<\/p>\n<p>\u5728\u6b64\u5efa\u8bae\u5e94\u7528\u5f00\u53d1\u8005\u4f7f\u7528\u7684\u9884\u89c8\u7a97\u53e3\u5bbd\u9ad8\u6bd4\u4f8b\u548cCamera\u8f93\u51fa\u56fe\u50cf\u7684\u5bbd\u9ad8\u6bd4\u4f8b\u5c3d\u91cf\u4fdd\u6301\u4e00\u81f4\uff0c\u8fd9\u6837\u9884\u89c8\u663e\u793a\u7684\u753b\u9762\u5373\u53ef\u4ee5\u6b63\u5e38\u663e\u793a\u3002\u4e3a\u4e86\u89e3\u51b3\u5e94\u7528\u9891\u7e41\u9002\u914d\u5de5\u4f5c\uff0c\u5e0c\u671b\u5f00\u53d1\u8005\u53c2\u8003\u6807\u51c6\u7684Camera\u9884\u89c8\u4f7f\u7528\u793a\u4f8b\u5b9e\u73b0\u529f\u80fd\u3002<\/p>\n<h2 id=\"_1\">2.Camera\u9884\u89c8\u63a5\u53e3\u4f7f\u7528\u793a\u4f8b<\/h2>\n<h3 id=\"_1_0\">2.1.\u83b7\u53d6Camera\u652f\u6301\u7684\u9884\u89c8\u56fe\u50cf\u8f93\u51fa\u5206\u8fa8\u7387<\/h3>\n<pre class=\"wp-block-code\"><code><em>\/\/Camera API1<\/em>\nCamera mCamera = Camera.open(cameraId);\nList&lt;Size&gt; supportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes()\n \n<em>\/\/Camera API2<\/em>\nSize&#091;] supportedPreviewSizes = CameraManager.getCameraCharacteristics(cameraId).get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(klass)<\/code><\/pre>\n<h3 id=\"_1_1\">2.2.\u6839\u636e\u9884\u89c8\u7a97\u53e3Surface\u7684\u5bbd\u9ad8\u6765\u9009\u53d6\u6700\u4f73Camera\u56fe\u50cf\u8f93\u51fa\u5c3a\u5bf8<\/h3>\n<ul>\n<li>Camera API1\uff1a<\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code><em>\/\/\u83b7\u53d6\u9884\u89c8\u7a97\u53e3\u7684\u5bbd\u9ad8<\/em>\n<strong>protected<\/strong> <strong>void<\/strong> <strong>onMeasure<\/strong>(<strong>int<\/strong> widthMeasureSpec, <strong>int<\/strong> heightMeasureSpec) {\n    <em>\/\/ We purposely disregard child measurements because act as a<\/em>\n    <em>\/\/ wrapper to a SurfaceView that centers the camera preview instead<\/em>\n    <em>\/\/ of stretching it.<\/em>\n    <strong>final<\/strong> <strong>int<\/strong> width = resolveSize(getSuggestedMinimumWidth(),\n            widthMeasureSpec);\n    <strong>final<\/strong> <strong>int<\/strong> height = resolveSize(getSuggestedMinimumHeight(),\n            heightMeasureSpec);\n    setMeasuredDimension(width, height);\n \n    <strong>if<\/strong> (mSupportedPreviewSizes != <strong>null<\/strong>) {\n        mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width,\n                height);\n    }\n \n    <strong>if<\/strong> (mCamera != <strong>null<\/strong>) {\n      Camera.Parameters parameters = mCamera.getParameters();\n      parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);\n \n      mCamera.setParameters(parameters);\n    }\n}<\/code><\/pre>\n<pre class=\"wp-block-code\"><code><em>\/\/\u5f97\u5230\u6700\u4f73Camera\u56fe\u50cf\u8f93\u51fa\u5c3a\u5bf8<\/em>\n<strong>public<\/strong> Camera.Size <strong>getOptimalSize<\/strong>(@NonNull List&lt;Camera.Size&gt; sizes, <strong>int<\/strong> w, <strong>int<\/strong> h) {\n    <strong>final<\/strong> <strong>double<\/strong> ASPECT_TOLERANCE = 0.1;\n    <strong>double<\/strong> targetRatio = (<strong>double<\/strong>) w \/ h;\n    Camera.Size optimalSize = <strong>null<\/strong>;\n    <strong>double<\/strong> minDiff = Double.MAX_VALUE;\n \n    <strong>int<\/strong> targetHeight = h;\n \n    <strong>for<\/strong> (Camera.Size size : sizes) {\n      <strong>double<\/strong> ratio = (<strong>double<\/strong>) size.width \/ size.height;\n      <strong>if<\/strong> (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE) <strong>continue<\/strong>;\n      <strong>if<\/strong> (Math.abs(size.height - targetHeight) &lt; minDiff) {\n        optimalSize = size;\n        minDiff = Math.abs(size.height - targetHeight);\n      }\n    }\n \n    <strong>if<\/strong> (optimalSize == <strong>null<\/strong>) {\n      minDiff = Double.MAX_VALUE;\n      <strong>for<\/strong> (Camera.Size size : sizes) {\n        <strong>if<\/strong> (Math.abs(size.height - targetHeight) &lt; minDiff) {\n          optimalSize = size;\n          minDiff = Math.abs(size.height - targetHeight);\n        }\n      }\n    }\n \n    <strong>return<\/strong> optimalSize;\n  }<\/code><\/pre>\n<p>\u8be6\u7ec6\u5b9e\u73b0\u89c1\uff1ahttps:\/\/cs.android.com\/android\/platform\/superproject\/+\/master:development\/samples\/HoneycombGallery\/src\/com\/example\/android\/hcgallery\/CameraFragment.java<\/p>\n<ul>\n<li>Camera API2:<\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code><em>\/\/\u83b7\u53d6\u9884\u89c8\u7a97\u53e3\u7684\u5bbd\u9ad8<\/em>\n<strong>private<\/strong> <strong>final<\/strong> TextureView.SurfaceTextureListener mSurfaceTextureListener\n        = <strong>new<\/strong> TextureView.SurfaceTextureListener() {\n \n    @Override\n    <strong>public<\/strong> <strong>void<\/strong> <strong>onSurfaceTextureAvailable<\/strong>(SurfaceTexture texture, <strong>int<\/strong> width, <strong>int<\/strong> height) {\n        openCamera(width, height);\n    }\n \n    @Override\n    <strong>public<\/strong> <strong>void<\/strong> <strong>onSurfaceTextureSizeChanged<\/strong>(SurfaceTexture texture, <strong>int<\/strong> width, <strong>int<\/strong> height) {\n        configureTransform(width, height);\n    }\n \n    @Override\n    <strong>public<\/strong> <strong>boolean<\/strong> <strong>onSurfaceTextureDestroyed<\/strong>(SurfaceTexture texture) {\n        <strong>return<\/strong> <strong>true<\/strong>;\n    }\n \n    @Override\n    <strong>public<\/strong> <strong>void<\/strong> <strong>onSurfaceTextureUpdated<\/strong>(SurfaceTexture texture) {\n    }\n \n};<\/code><\/pre>\n<p><a href=\"https:\/\/app.applebyme.cn\/shengcheng-29.html\">h5\u751f\u6210app<\/a><\/p>\n<pre class=\"wp-block-code\"><code><em>\/\/\u5f97\u5230\u6700\u4f73Camera\u56fe\u50cf\u8f93\u51fa\u5c3a\u5bf8<\/em>\n<strong>private<\/strong> <strong>static<\/strong> Size <strong>chooseOptimalSize<\/strong>(Size&#091;] choices, <strong>int<\/strong> textureViewWidth,\n        <strong>int<\/strong> textureViewHeight, <strong>int<\/strong> maxWidth, <strong>int<\/strong> maxHeight, Size aspectRatio) {\n \n    <em>\/\/ Collect the supported resolutions that are at least as big as the preview Surface<\/em>\n    List&lt;Size&gt; bigEnough = <strong>new<\/strong> ArrayList&lt;&gt;();\n    <em>\/\/ Collect the supported resolutions that are smaller than the preview Surface<\/em>\n    List&lt;Size&gt; notBigEnough = <strong>new<\/strong> ArrayList&lt;&gt;();\n    <strong>int<\/strong> w = aspectRatio.getWidth();\n    <strong>int<\/strong> h = aspectRatio.getHeight();\n    <strong>for<\/strong> (Size option : choices) {\n        <strong>if<\/strong> (option.getWidth() &lt;= maxWidth &amp;&amp; option.getHeight() &lt;= maxHeight &amp;&amp;\n                option.getHeight() == option.getWidth() * h \/ w) {\n            <strong>if<\/strong> (option.getWidth() &gt;= textureViewWidth &amp;&amp;\n                option.getHeight() &gt;= textureViewHeight) {\n                bigEnough.add(option);\n            } <strong>else<\/strong> {\n                notBigEnough.add(option);\n            }\n        }\n    }\n \n    <em>\/\/ Pick the smallest of those big enough. If there is no one big enough, pick the<\/em>\n    <em>\/\/ largest of those not big enough.<\/em>\n    <strong>if<\/strong> (bigEnough.size() &gt; 0) {\n        <strong>return<\/strong> Collections.min(bigEnough, <strong>new<\/strong> CompareSizesByArea());\n    } <strong>else<\/strong> <strong>if<\/strong> (notBigEnough.size() &gt; 0) {\n        <strong>return<\/strong> Collections.max(notBigEnough, <strong>new<\/strong> CompareSizesByArea());\n    } <strong>else<\/strong> {\n        Log.e(TAG, \"Couldn't find any suitable preview size\");\n        <strong>return<\/strong> choices&#091;0];\n    }\n}<\/code><\/pre>\n<p>\u8be6\u7ec6\u5b9e\u73b0\u89c1\uff1ahttps:\/\/cs.android.com\/android\/platform\/superproject\/+\/master:development\/samples\/browseable\/Camera2Basic\/src\/com.example.android.camera2basic\/Camera2BasicFragment.java<\/p>\n<h3 id=\"_1_2\">2.3.\u5c06\u83b7\u53d6\u5230\u7684\u6700\u4f73\u56fe\u50cf\u8f93\u51fa\u5c3a\u5bf8\u8bbe\u7f6e\u5230Camera<\/h3>\n<pre class=\"wp-block-code\"><code><em>\/\/Camera API1<\/em>\nmCameraParameters.setPreviewSize(size.getWidth(), size.getHeight())\n \n<em>\/\/Camera API2<\/em>\ntexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight())<\/code><\/pre>\n<p>                              <a href=\"https:\/\/app.applebyme.cn\/shengcheng-42.html\">app\u4e00\u952e\u751f\u6210<\/a>                          <\/p>\n<p>\u7f16\u8f91\uff1ayimen\uff0c\u5982\u82e5\u8f6c\u8f7d\uff0c\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttps:\/\/www.yimenapp.com\/kb-yimen\/12683\/<\/p>\n<p>\u90e8\u5206\u5185\u5bb9\u6765\u81ea\u7f51\u7edc\u6295\u7a3f\uff0c\u5982\u6709\u4fb5\u6743\u8054\u7cfb\u7acb\u5220<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0f\u7c73\u5f00\u53d1\u5e73\u53f0\u5e94\u7528\u8c03\u7528Camera\u9884\u89c8\u6548\u679c\u9002\u914d\u8bf4\u660e1.\u5e94\u7528\u4e2d\u62cd\u6444\u6216\u626b\u4e00\u626b\u529f\u80fd\u9884\u89c8\u9875\u9762\u51fa\u73b0\u62c9\u4f38\u73b0\u8c61\u7531\u4e8e\u4e0d\u540c\u624b\u673a\u5c4f\u5e55\u5c3a\u5bf8\u6bd4\u4f8b\u4e0d\u5c3d\u76f8\u540c\uff0c\u6216\u8bb8\u5e94\u7528\u5728\u8c03\u7528Camera\u9884\u89c8\u5b9e\u73b0\u65b9\u6cd5\u4e0d\u6807\u51c6\u6216\u7b2c\u4e09\u65b9SDK\u7ef4\u62a4\u4e0d\u53ca\u65f6\uff0c\u63d0\u4f9b\u7684\u9884\u89c8\u7a97\u53e3\u5bbd\u9ad8\u6bd4\u4f8b\u548cCamera\u8f93\u51fa\u56fe\u50cf\u7684\u5bbd\u9ad8\u6bd4\u4f8b\u4e0d\u4e00\u81f4\uff0c\u4ee5\u81f3\u4e8e\u5e94\u7528\u5728\u4f7f\u7528\u62cd\u6444\u6216\u626b\u4e00\u626b\u529f\u80fd\u65f6\u9884\u89c8\u9875<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[548,824,1326],"class_list":["post-1912","post","type-post","status-publish","format-standard","hentry","category-appshangjia","tag-548","tag-824","tag-1326"],"_links":{"self":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/1912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/comments?post=1912"}],"version-history":[{"count":0,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/1912\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/media?parent=1912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/categories?post=1912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/tags?post=1912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}