【发布时间】:2016-04-11 16:25:35
【问题描述】:
我看到了有关使用本机相机和为我们自己的应用程序拍摄照片的各种链接。但是我很难将照片保存到设备或存储在数据库中。我有包含文件路径等的代码。但我不知道在程序中的何处包含这些代码。请帮帮我。
我已使用以下 URL 实现相机 http://docs.phonegap.com/en/2.0.0/cordova_camera_camera.md.html
我正在使用 worklight 和 apache cordova 来调用相机。
File sdDir = Environment.getExternalStorageDirectory();
在脚本中的何处包含上述行?
如何以及在何处将 img 标签包含在 HTML 中?
如何将图片转成base64并存入数据库?
我附上了下面的代码。请交叉检查我使用的功能并帮助我。
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// PhoneGap is ready to be used!
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64 encoded image data
// console.log(imageData);
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
largeImage.src = imageURI;
}
// A button will call this function
//
function capturePhoto() {
String fileName = System.currentTimeMillis()+".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onSuccess, onFail, { quality: 50 });
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
}
// A button will call this function
//
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
【问题讨论】:
-
这需要一个详尽的答案,您是否正确使用了链接中所述的 navaigator.camera.getpicture() ?你能拍张照片吗!如果是,请在提供的问题中分享您的代码。我是否正确假设您想获取捕获的图像的 uri?您想要手机中图像的位置,并且您想在屏幕上显示您的图像和/或将其保存到数据库中?现在你必须 imaplement websql/sqlite 存储插入操作。使用代码示例恢复并编辑您的问题
-
@IamKarim1992 请在下面找到代码并提供帮助。
-
@IamKarim1992 你说得对,我想在 HTML 中显示图像并存储在在线数据或 mysql 中。如果捕获的图片在新页面中,那就更好了。
-
先把你的代码拿来拍照,可以找到很多关于如何使用 navigator.camera.getpicture(onSuccess,onError,options) 的例子,然后从那里开始工作。跨度>
-
我可以拍照,但不能像我之前提到的那样显示或说出来。 @IamKarim1992
标签: android cordova ibm-mobilefirst