1

サーバーにnode.jsを使用し、アプリにjavascript、jquery、phonegap(現在はcordova 1.6.0と呼ばれています)を使用して、ハイブリッドAndroidアプリを部分的に開発しました。

アプリの一部では、ユーザーはスマートフォンのカメラを使用して写真を撮り、それをサーバーにアップロードできます。

画像をアップロードするアプリの私のコードは...

    var imageURI = document.getElementById('display_image').src;
    if (!imageURI) { // || (self.elem.image_play.style.display == "none")) {
        console.log('no image uri defined - ' + JSON.stringify(imageURI));
        //document.getElementById('camera_status').innerHTML = "Take picture or select picture from library first.";
        return;
    }

    // Verify server has been entered
    server = 'http://192.168.1.3:8180/newimage/';
    if (server) {
        console.log("starting upload");
        // Specify transfer options
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType="image/jpg";
        options.chunkedMode = false;

        // Transfer picture to server
        var ft = new FileTransfer();
        ft.upload(imageURI, server, function(r) {
            console.log("upload successful"+r.bytesSent+" bytes uploaded.");
            //document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
        }, function(error) {
            console.log("upload failed - Error Code = "+error.code);
            //document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code;
        }, options);
    }

これは正常に動作しますが、node.js サーバーで画像を処理する方法がわかりません。現在、私のコードは次のとおりです。

'/newimage/': function(req,res){

    var chunks;

    req.addListener('data', function (chunk) {
        console.log('in data processing');
        chunks.push(chunk);
    });

    req.addListener('end', function () {
        console.log('completing data processing');
        fs.writeFile("out.jpg", chunks, function(err) {
            console.log(err);
        });
    });
}

このサーバー メソッドは実行されますが、console.log 行は実行されません。

私はこれを整理するために2日間試みており、さまざまな方法を試しましたが、phonegap ft.uploadメソッドから受信した画像ファイルを処理する方法がわかりません. 誰でもこれを手伝ってもらえますか?

これを整理したら、最終的な目的は、knox モジュールを使用できると思われる Amazon s3 ストレージに画像ファイルをアップロードすることです。

4

1 に答える 1

1

node formidableモジュールとfsモジュールを使用して、アップロードされたイメージをnodejsサーバーの/ tmpフォルダーに書き込み、後でOCRを実行します。コードは、 www.nodebeginner.orgブックからの画像アップロードの例に基づいています。

uploadPicture()メソッドのphonegapプロジェクトのJSコード:

 // Specify transfer options
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType="image/jpeg"
        options.chunkedMode = false;

        // Transfer picture to server
        var ft = new FileTransfer();
        ft.upload(imageURI, server, function(r) {
            document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";              
        }, function(error) {
            document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code;               
        }, options, true);

ノードサーバー上のrequesthandler.jsの私のコード:

function upload(response, request) {
    console.log("Request handler 'upload' was called.");
    var form = new formidable.IncomingForm();
    form.parse(request, function(error, fields, files) {

        //logs the file information 
        console.log(JSON.stringify(files))

        fs.rename(files.file.path, "/tmp/test.png", function(err) {
            if (err) {
                fs.unlink("/tmp/test.png");
                fs.rename(files.file.path, "/tmp/test.png");
            }
        });
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write("received image:<br/>");
        response.write("<img src='/show' />");
        response.end();
    });
}
于 2013-02-13T23:20:33.570 に答える