Phonegap 摄像头不工作
Phonegap camera doesn't work
我的 Android 应用程序需要拍照,但相机插件不起作用。当我点击按钮时没有任何反应。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"/>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
alert("ready");
}
function capturePhoto()
{
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI });
}
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
}
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto()">Capture</button> <br>
<button onclick="onDeviceReady()">alert</button> <br>
<img id="myImage" src="" />
</body>
</html>
我添加第二个按钮只是为了检查按钮是否有效(确实有效)。
config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns= "http://www.w3.org/ns/widgets"
xmlns:gap= "http://phonegap.com/ns/1.0"
id= "testaplikacji"
versionCode= "1"
version = "1.0.0" >
<name>Camera</name>
<description>Camera test</description>
<author>Sebastian</author>
<gap:platform name="android" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.camera" />
<gap:plugin name="cordova-plugin-camera" />
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
<access origin="*"/>
<gap:config-file platform="android" parent="/manifest" mode="add" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</gap:config-file>
</widget>
我试过有权限和没有权限。我尝试了不同的插件。我试过这个要点:https://gist.github.com/dhavaln/2238017
还有本教程中的代码:
https://www.youtube.com/watch?v=KlBfmHDZjmg
没有任何效果。我浪费了很多时间来尝试使它起作用。请帮我。
您在两个不同的地方使用了一个名为 image 的变量。但这是两个不同的变量。在 onSuccess 函数中获取您需要的图像引用。
function onSuccess(imageURI)
{
var image = document.getElementById('myImage');
image.src = imageURI;
}
我已经使用 phonegap 构建服务器对此进行了测试,当您将这个最小 config.xml
文件与您的 html 文件。另外,请确保不要将 Zip 存档中的 phonegap.js
上传到构建服务器。在构建过程中会自动添加一个额外的。
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "com.Whosebug.example.SO37227132"
versionCode = "10"
version = "1.0.0" >
<!-- versionCode is optional and Android only -->
<name>PhoneGap Example</name>
<description>
An example for phonegap build docs.
</description>
<author href="https://build.phonegap.com" email="support@phonegap.com">
Hardeep Shoker
</author>
<platform name="android" />
<plugin name="org.apache.cordova.camera" source="pgb" />
</widget>
我的 Android 应用程序需要拍照,但相机插件不起作用。当我点击按钮时没有任何反应。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"/>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
alert("ready");
}
function capturePhoto()
{
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI });
}
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
}
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto()">Capture</button> <br>
<button onclick="onDeviceReady()">alert</button> <br>
<img id="myImage" src="" />
</body>
</html>
我添加第二个按钮只是为了检查按钮是否有效(确实有效)。
config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns= "http://www.w3.org/ns/widgets"
xmlns:gap= "http://phonegap.com/ns/1.0"
id= "testaplikacji"
versionCode= "1"
version = "1.0.0" >
<name>Camera</name>
<description>Camera test</description>
<author>Sebastian</author>
<gap:platform name="android" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.camera" />
<gap:plugin name="cordova-plugin-camera" />
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
<access origin="*"/>
<gap:config-file platform="android" parent="/manifest" mode="add" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</gap:config-file>
</widget>
我试过有权限和没有权限。我尝试了不同的插件。我试过这个要点:https://gist.github.com/dhavaln/2238017
还有本教程中的代码: https://www.youtube.com/watch?v=KlBfmHDZjmg
没有任何效果。我浪费了很多时间来尝试使它起作用。请帮我。
您在两个不同的地方使用了一个名为 image 的变量。但这是两个不同的变量。在 onSuccess 函数中获取您需要的图像引用。
function onSuccess(imageURI)
{
var image = document.getElementById('myImage');
image.src = imageURI;
}
我已经使用 phonegap 构建服务器对此进行了测试,当您将这个最小 config.xml
文件与您的 html 文件。另外,请确保不要将 Zip 存档中的 phonegap.js
上传到构建服务器。在构建过程中会自动添加一个额外的。
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "com.Whosebug.example.SO37227132"
versionCode = "10"
version = "1.0.0" >
<!-- versionCode is optional and Android only -->
<name>PhoneGap Example</name>
<description>
An example for phonegap build docs.
</description>
<author href="https://build.phonegap.com" email="support@phonegap.com">
Hardeep Shoker
</author>
<platform name="android" />
<plugin name="org.apache.cordova.camera" source="pgb" />
</widget>