用相机拍摄照片并存储在DB Phonegap中

时间:2012-12-13 13:56:50

标签: sqlite cordova

我正在尝试使用Phonegap(Cordova 2.2)构建应用程序。我对代码和js相当新,所以如果我的一些问题看起来很糟糕,请耐心等待。我的问题有几个部分: 我希望用户能够输入名称和图像(使用设备相机)来创建他们的个人资料。我已经在模拟器上进行了测试,到目前为止他们可以输入他们的名字并用相机拍照。这些都在完成时显示在模拟器中。但是,显然我想保存那个细节,所以,我创建了一个DB(从阅读开始,本地存储的大小有限)。 1.数据库是否正确完成?我既没有错误也没有成功警报。 我正在努力解决将信息传递到数据库的逻辑。我猜我在click事件上调用了populate_UsersDB()函数,需要编写某种INSERT INTO userProfiles VALUE的东西。另外,我可以直接从onPhotoFileSuccess()函数中获取Imagedata并从那里将其发送到数据库中。 3.如果我不想要一个完成按钮(而不是“点击此处拍照”占位符),我如何测试操作是否完成并将信息发送到数据库。从阅读开始,我想我可以使用onChange(),但不确定。另外,猜测我必须使用帖子或到某个地方?

抱歉,我知道这个问题中有很多问题。非常感谢任何建议或支持。谢谢。这是代码。

    //Use the device camera to capture an image of the user

var pictureSource; //图片来源     var destinationType; //设置返回值的格式

// 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) {
  // 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 onPhotoFileSuccess(imageData) {
  // Get image handle
  console.log(JSON.stringify(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 = imageData;
}

// A button will call this function
function capturePhotoWithData() {
  // Take picture using device camera and retrieve image as base64-encoded string
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality:50, destinationType:Camera.DestinationType.DATA_URL });
}
function getPhoto(source) {
  // Retrieve image file location from specified source
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
    destinationType: destinationType.FILE_URI,
    sourceType: source });
}

// Called if something goes wrong.
function onFail(message) {
  alert('Failed because: ' + message);
}

//CREATE THE DATABASE
document.addEventListener("deviceready", onDeviceReady, false);
    var db = window.openDatabase("Users_DB", "1.0", "User Profiles DB", 200000); //will create database or open it

    //function will be called when device ready
    function onDeviceReady(){
        db.transaction(populateUsers_DB, errorCB, successCB);
    }
    //create table
    function populateUsers_DB(tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS userProfiles (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Image BLOB)');
        tx.executeSql('INSERT INTO userProfiles(Name) VALUE ("")');
    }
    //function will be called when an error occurred
    function errorCB(err) {
        alert("Error processing SQL: "+err.code);
    }
    //function will be called when process succeed
    function successCB() {
        alert("success!");
        db.transaction(queryDB,errorCB);
    }
    //select all from userProfiles
    function queryDB(tx){
        tx.executeSql('SELECT * FROM userProfiles',[],querySuccess,errorCB);
    }
    function querySuccess(tx,result){
        $('#userList').empty();
        $.each(result.rows,function(index){
            var row = result.rows.item(index);
            $('#userList').append('<li><a href="#"><h3 class="ui-li-heading">'+row['Image']+'</h3><p class="ui-li-desc">Name '+row['Name']+'</p></a></li>');
        });
        $('#userList').listview();
    }

0 个答案:

没有答案