{"id":12683,"date":"2022-11-24T15:12:41","date_gmt":"2022-11-24T07:12:41","guid":{"rendered":"https:\/\/www.yimenapp.com\/kb-yimen\/?p=12683"},"modified":"2022-11-24T15:12:42","modified_gmt":"2022-11-24T07:12:42","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:\/\/www.yimenapp.com\/kb-yimen\/12683\/","title":{"rendered":"\u5c0f\u7c73\u5f00\u53d1\u5e73\u53f0\u5e94\u7528\u8c03\u7528Camera\u9884\u89c8\u6548\u679c\u9002\u914d\u8bf4\u660e"},"content":{"rendered":"\n<p><span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e5%b0%8f%e7%b1%b3%e5%bc%80%e5%8f%91%e5%b9%b3%e5%8f%b0\/\" title=\"\u5c0f\u7c73\u5f00\u53d1\u5e73\u53f0\" target=\"_blank\">\u5c0f\u7c73\u5f00\u53d1\u5e73\u53f0<\/a><\/span>\u5e94\u7528<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e8%b0%83%e7%94%a8camera\/\" title=\"\u8c03\u7528Camera\" target=\"_blank\">\u8c03\u7528Camera<\/a><\/span>\u9884\u89c8\u6548\u679c\u9002\u914d\u8bf4\u660e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" 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\n\n\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\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/camera%e9%a2%84%e8%a7%88\/\" title=\"Camera\u9884\u89c8\" target=\"_blank\">Camera\u9884\u89c8<\/a><\/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\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/camera\/\" title=\"Camera\" target=\"_blank\">Camera<\/a><\/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\n\n\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\n\n\n<h2 class=\"wp-block-heading\" id=\"_1\">2.Camera\u9884\u89c8\u63a5\u53e3\u4f7f\u7528\u793a\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"_1_0\">2.1.\u83b7\u53d6Camera\u652f\u6301\u7684\u9884\u89c8\u56fe\u50cf\u8f93\u51fa\u5206\u8fa8\u7387<\/h3>\n\n\n\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&#91;] supportedPreviewSizes = CameraManager.getCameraCharacteristics(cameraId).get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(klass)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" 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\n\n\n<ul class=\"wp-block-list\"><li>Camera API1\uff1a<\/li><\/ul>\n\n\n\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\n\n\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\n\n\n<p>\u8be6\u7ec6\u5b9e\u73b0\u89c1\uff1a<a href=\"https:\/\/cs.android.com\/android\/platform\/superproject\/+\/master:development\/samples\/HoneycombGallery\/src\/com\/example\/android\/hcgallery\/CameraFragment.java\">https:\/\/cs.android.com\/android\/platform\/superproject\/+\/master:development\/samples\/HoneycombGallery\/src\/com\/example\/android\/hcgallery\/CameraFragment.java<\/a><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Camera API2:<\/li><\/ul>\n\n\n\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\n\n\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&#91;] 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&#91;0];\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8be6\u7ec6\u5b9e\u73b0\u89c1\uff1a<a href=\"https:\/\/cs.android.com\/android\/platform\/superproject\/+\/master:development\/samples\/browseable\/Camera2Basic\/src\/com.example.android.camera2basic\/Camera2BasicFragment.java\">https:\/\/cs.android.com\/android\/platform\/superproject\/+\/master:development\/samples\/browseable\/Camera2Basic\/src\/com.example.android.camera2basic\/Camera2BasicFragment.java<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"_1_2\">2.3.\u5c06\u83b7\u53d6\u5230\u7684\u6700\u4f73\u56fe\u50cf\u8f93\u51fa\u5c3a\u5bf8\u8bbe\u7f6e\u5230Camera<\/h3>\n\n\n\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","protected":false},"excerpt":{"rendered":"<p>\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<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,94,160],"tags":[5919,5921,862,5772,5789,5920,5922],"special":[67],"attr":[],"class_list":{"0":"post-12683","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-appsj","7":"category-appjc","8":"category-appcs","9":"tag-camera","11":"tag-xiaomi","12":"tag-5772","13":"tag-5789","14":"tag-5920","16":"special-xiaomikfpt","17":"entry"},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/12683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/comments?post=12683"}],"version-history":[{"count":1,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/12683\/revisions"}],"predecessor-version":[{"id":12684,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/12683\/revisions\/12684"}],"wp:attachment":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/media?parent=12683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/categories?post=12683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/tags?post=12683"},{"taxonomy":"special","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/special?post=12683"},{"taxonomy":"attr","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/attr?post=12683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}