简体   繁体   English

将捕获的图像插入数据库问题(Cordova / SQlite / JavaScript)

[英]Inserting captured image into database issue (Cordova/SQlite/JavaScript)

I am attempting to store captured images into my database however I cannot seem to get the actual data of the image. 我试图将捕获的图像存储到数据库中,但是似乎无法获取图像的实际数据。

Once the camera takes a photo it should be automatically stored, however I have the query alert, and it shows as the image being undefined. 相机拍摄完照片后,应将其自动存储,但是我有查询警报,它显示为图像未定义。

I'm sure there are a few problems with my coding, as my coding isn't the best. 我敢肯定我的编码有一些问题,因为我的编码不是最好的。

alert(_Query3); 警报(_Query3); gives me the following: 给我以下内容:

INSERT INTO Gallery(myImage) values ('undefined')

JavaScript: JavaScript:

   document.addEventListener("deviceready", onDeviceReady, false);
          var db;
          function onDeviceReady() {
            db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2 * 1024 * 1024);
            db.transaction(function(tx) {
           tx.executeSql('CREATE TABLE IF NOT EXISTS Gallery (id INTEGER PRIMARY KEY, myImage BLOB)');
            }, errorE, successS);
          }

function successS() {
  alert("Camera database ready!");
  document.getElementById("btnCamera").onclick = function() {
    navigator.camera.getPicture(onSuccess, onFail, {
      quality: 50,
      destinationType: Camera.DestinationType.DATA_URL
    });
  };
}

function onSuccess(tx, imageData) {
alert("Camera test 1");
  var image = document.getElementById("lastPhoto");
  image.src = "data:image/jpeg;base64," + imageData;
  base64imageData = imageData;
  var _Query3 = ("INSERT INTO Gallery(myImage) values ('"+ base64imageData +"')");
  alert(_Query3);
  tx.executeSql(_Query3);
}
            /*   function successCamera() {
               navigator.notification.alert("Image has been stored", null, "Information", "ok");
                 $( ":mobile-pagecontainer" ).pagecontainer( "change", "#page4" );
             } */

function onFail(message) {
  alert('Failed because: ' + message);
}

function errorE(err) {
  alert("Error processing SQL: " + err.code);
}

I might have found a solution to your problem. 我可能已经找到解决您问题的方法。 Your onSuccess Callback seems to be of the wrong syntax. 您的onSuccess回调似乎语法错误。 I don't understand how the "tx" variable can be used with the imageData variable because only a single parameter can be used for the onSuccess as far as I know. 我不了解如何将“ tx”变量与imageData变量一起使用,因为据我所知,只能将一个参数用于onSuccess。 If my assumptions are correct, then the Base64 encoding of the image data must be stored in the tx parameter. 如果我的假设是正确的,那么图像数据的Base64编码必须存储在tx参数中。

Let me know if this has managed to solve your issue. 让我知道这是否能够解决您的问题。

You can refer to camera.onSuccess for the proper declaration. 您可以参考camera.onSuccess以获得正确的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM