{"id":8677,"date":"2024-05-30T19:01:01","date_gmt":"2024-05-30T11:01:01","guid":{"rendered":""},"modified":"2024-05-30T19:01:01","modified_gmt":"2024-05-30T11:01:01","slug":"\u7528\u4e8e android \u7684 camera connect_\u5b89\u5353\u7cfb\u7edf\u76f8\u673a\u6743\u9650\u8bbe\u7f6e\u5728\u54ea","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8677.html","title":{"rendered":"\u7528\u4e8e android \u7684 camera connect_\u5b89\u5353\u7cfb\u7edf\u76f8\u673a\u6743\u9650\u8bbe\u7f6e\u5728\u54ea"},"content":{"rendered":"
\u5f53\u6211\u4eec\u4f7f\u7528\u624b\u673a\u76f8\u673a\u7684\u65f6\u5019\uff0c\u901a\u5e38\u5c31\u4f1a\u60f3\u5230\u5148\u83b7\u53d6\u624b\u673a\u76f8\u673a\u6743\u9650\uff0c\u4e5f\u5c31\u662f\u8fd0\u884c\u65f6\u6743\u9650\u3002<\/p>\n
\u4e5f\u5c31\u662f\u7c7b\u4f3c\u4e0b\u9762\u65b9\u6cd5<\/p>\n
\/**\n * \u7533\u8bf7\u76f8\u673a\u6743\u9650\n *\/\n private void startRequestPermission(){\n \/\/321\u4e3a\u8bf7\u6c42\u7801\n ActivityCompat.requestPermissions(this,PERMISSIONS_STORAGE,321);\n }\n @Override\n protected void onActivityResult(int requestCode, int resultCode, Intent data) {\n super.onActivityResult(requestCode, resultCode, data);\n if (requestCode==CODE_CAMERA_REQUEST&&resultCode==RESULT_OK){\n \/\/TODO \n }\n }<\/code><\/pre>\n\u4f46\u662f\u5bf9\u4e8eAndroid 6.0\u4ee5\u4e0b\u7684\u624b\u673a\uff0c\u90fd\u662f\u9ed8\u8ba4\u62e5\u6709\u8fd9\u4e2a\u6743\u9650\u7684\uff0c\u4f46\u5982\u679c\u5c06\u624b\u673a\u6743\u9650\u5173\u95ed\u6389\uff0c\u540c\u6837\u5728\u4ee3\u7801\u4e0a\u8fd8\u662f\u8fd4\u56detrue\uff0c\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7try{}catch()\u7684\u65b9\u5f0f\uff0c\u4e5f\u5c31\u662f\u901a\u8fc7\u4ee3\u7801\u53bb\u4f7f\u7528\u76f8\u673a\uff0c\u7136\u540e\u6355\u83b7\u5f02\u5e38\uff0c\u5982\u679c\u76f8\u673a\u6743\u9650\u6ca1\u6709\uff0c\u5c31\u4f1a\u8d70\u5230\u4f60\u7684\u5f02\u5e38\u5904\u7406\u91cc\u9762\uff0c\u4e5f\u5c31\u662f\u8bf4\u6ca1\u6709\u8fd9\u4e2a\u6743\u9650\uff0c\u4e5f\u53ef\u4ee5\u770b\u770b\u4e0b\u9762\u65b9\u5f0f\u3002<\/p>\n
\/**\n * \u6444\u50cf\u5934\u662f\u5426\u53ef\u7528\n *\/\n public static boolean isCameraUseable() {\n Camera mCamera = null;\n try {\n mCamera = Camera.open();\n \/\/ setParameters \u662f\u9488\u5bf9\u9b45\u65cfMX5\u3002MX5\u901a\u8fc7Camera.open()\u62ff\u5230\u7684Camera\u5bf9\u8c61\u4e0d\u4e3anull\n Camera.Parameters mParameters = mCamera.getParameters();\n mCamera.setParameters(mParameters);\n } catch (Exception e) {\n return false;\n } finally {\n if (mCamera != null) {\n try {\n mCamera.release();\n Thread.sleep(100);\n } catch (Exception e) {\n return false;\n }\n }\n }\n return true;\n }<\/code><\/pre>\n\u8fd9\u6837\u7684\u8bdd\u5c31\u57fa\u672c\u5904\u7406\u4e86\u5927\u90e8\u5206\u624b\u673a\uff0c\u4f46\u662f\u6700\u8fd1\u78b0\u5230\u4e00\u4e2avivo\u7684\u624b\u673a\uff0ccamera\u6743\u9650\u62d2\u7edd\uff0c\u4f46\u662f\u8fd9\u4e2a\u5224\u65ad\u6444\u50cf\u5934\u662f\u5426\u53ef\u7528\u8fd4\u56de\u7684true\uff0c\u4e5f\u5c31\u662f\u8fdb\u5165\u5230surfaceView\u6e32\u67d3\u5c31\u4e00\u56e2\u9ed1\uff0c\u8fd9\u65f6\u77ac\u95f4\u5c34\u5c2c\uff0c\u611f\u89c9\u8fd9\u4e2a\u624b\u673a\u5c31\u50cf\u662f\u534e\u5f3a\u5317\u7ec4\u88c5\u56de\u6765\u4e00\u6837\uff0c\u4f46\u662f\u95ee\u9898\u80af\u5b9a\u6709\u89e3\u51b3\u65b9\u5f0f\uff0c\u540e\u6765debug\u65f6\u5019\u5c31\u770b\u5230\u4e00\u4e2aCmaera\u7c7b\u3002<\/p>\n
<\/p>\n
\u4e0d\u77e5\u53d1\u73b0\u4e86\u6ca1\u6709\uff0c\u8fd9\u4e2a\u91cc\u9762\u6709\u4e2a\u53eb\u505amHasPermission\u7684\u53d8\u91cf\uff0c\u521a\u597d\u8fd8\u662ffalse\uff0c\u7136\u540e\u6211\u91cd\u65b0debug\uff0c\u53bb\u540c\u610f\u4f7f\u7528\u76f8\u673a\uff0c\u7136\u540e\u5c31\u53d1\u73b0\u8fd9\u4e2a\u503c\u53d8\u4e3atrue\uff0c\u8fd9\u5c31\u8ba9\u6211\u60f3\u5230\u4e86\u65b9\u5f0f\uff0c\u53bb\u4f7f\u7528\u8fd9\u4e2aCamera\u53bb\u70b9\u770b\u6709\u6ca1\u6709\u6210\u5458\u53d8\u91cf\u53eb\u505amHasPermission\u7684\uff0c\u7a81\u7136\u53d1\u73b0\u5e76\u6ca1\u6709\uff0c\u53bb\u8fd9\u4e2a\u6e90\u7801\u91cc\u9762\u770b\uff0c\u4e5f\u6ca1\u6709\u53d1\u73b0\uff0c\u5c31\u611f\u89c9\u9a6c\u4e0a\u8981\u89e3\u51b3\u7684\u95ee\u9898\u611f\u89c9\u597d\u50cf\u53c8\u9047\u5230\u95ee\u9898\u4e86\u3002\u8fd9\u4e2a\u5c31\u4e0d\u8981\u7d27\u4e86\u554a\u3002\u8fd9\u5c31\u60f3\u5230\u4e86\u901a\u8fc7\u53cd\u5c04\u53bb\u83b7\u53d6\u6210\u5458\u53d8\u91cf\uff0c\u6211\u4f7f\u7528\u7684\u662fgetDeclaredField()\u8fd9\u4e2a\u65b9\u6cd5\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u627e\u5230\u8fd9\u4e2a\u503c\u4e86\uff0c\u6ca1\u9519\uff0c\u4e0b\u9762\u5c31\u6765\u770b\u770b\u5177\u4f53\u5b9e\u73b0\u4e86\uff1a<\/p>\n
private boolean isHasPermission(Camera camera){\n try {\n Field fieldPassword = camera.getClass().getDeclaredField(\"mHasPermission\");\n fieldPassword.setAccessible(true);\n return (boolean)fieldPassword.get(camera);\n } catch (Exception e) {\n return true;\n }\n }<\/code><\/pre>\n\u5c31\u8fd9\u6837\u8fd4\u56de\u4e86mHasPermission\u7684value\u4e86\uff0c\u7ecf\u8fc7\u6d4b\u8bd5\uff0c\u5b8c\u7f8e\u5904\u7406\u4e86Android 6\u4ee5\u4e0b\u67d0\u4e9b\u624b\u673a\u4e0d\u80fd\u83b7\u53d6\u5230camera\u88ab\u62d2\u7edd\u7684\u76d1\u542c\u4e86\u3002<\/p>\n
<\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528\u4e8e android \u7684 camera connect_\u5b89\u5353\u7cfb\u7edf\u76f8\u673a\u6743\u9650\u8bbe\u7f6e\u5728\u54ea\u5f53\u6211\u4eec\u4f7f\u7528\u624b\u673a\u76f8\u673a\u7684\u65f6\u5019\uff0c\u901a\u5e38\u5c31\u4f1a\u60f3\u5230\u5148\u83b7\u53d6\u624b\u673a\u76f8\u673a\u6743\u9650\uff0c\u4e5f\u5c31...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8677"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8677"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8677\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}