7

これは簡単なはずですが、答えが見つかりません。サーバーにアップロードする前に、Phonegap の fileURI を介して画像の幅と高さを取得する必要があります。確かに、アップロードする前にこれを行う html5 / Phonegap マジックが必要です。私がどこにいるのかを示すために、いくつかの本当に縮小されたコードを次に示します。

function got_image(image_uri) {
    // Great, we've got the URI
    window.resolveLocalFileSystemURI(image_uri, function(fileEntry) {
        // Great, now we have a fileEntry object.
        // How do we get the image width and height?
    })
}

// Get the image URI using Phonegap
navigator.camera.getPicture(got_image, fail_function, some_settings)

欠落している部分は何か分かりますか? スピード ダイヤルにサイモン マクドナルドシャズロンがあればいいのにと思いますが、そうではありません。

4

5 に答える 5

8

ここに適切な解決策があります。

この関数に imageURI を渡します。URI は PhoneGap の getPicture() メソッドの両方から返されます。

このように: get_image_size_from_URI(imageURI)

function get_image_size_from_URI(imageURI) {
    // This function is called once an imageURI is rerturned from PhoneGap's camera or gallery function
    window.resolveLocalFileSystemURI(imageURI, function(fileEntry) {
        fileEntry.file(function(fileObject){
            // Create a reader to read the file
            var reader = new FileReader()

            // Create a function to process the file once it's read
            reader.onloadend = function(evt) {
                // Create an image element that we will load the data into
                var image = new Image()
                image.onload = function(evt) {
                    // The image has been loaded and the data is ready
                    var image_width = this.width
                    var image_height = this.height
                    console.log("IMAGE HEIGHT: " + image_height)
                    console.log("IMAGE WIDTH: " + image_width)
                    // We don't need the image element anymore. Get rid of it.
                    image = null
                }
                // Load the read data into the image source. It's base64 data
                image.src = evt.target.result
            }
            // Read from disk the data as base64
            reader.readAsDataURL(fileObject)
        }, function(){
            console.log("There was an error reading or processing this file.")
        })
    })
}
于 2013-02-28T19:47:37.273 に答える
3

次のコードは、今日の同じ問題を解決します (iOS でテスト済み)。

function got_image(image_uri)
{       
    $('<img src="'+image_uri+'"/>').on('load',function(){
        alert(this.naturalWidth +" "+ this.naturalHeight);
    });
}

それが役に立てば幸い!

于 2013-09-13T16:56:22.720 に答える
1

getPicture() のターゲットの幅と高さを使用して、最大寸法を制限できると思います。それらをサーバーに送信する必要がある場合は、サーバーコードがそれらのディメンションを取得するのに役立つと思います.

本当にjsでそれらのディメンションを取得する必要がある場合は、次のことができます

var img = new Image;
img.onload = function(){
  myCanvasContext.drawImage(img,0,0);
};
img.src = "data:YOUR_BASE64_DATA";

そして、あなたはimgでそれを行うことができます

于 2013-02-28T03:29:13.367 に答える
0

1.Phonegapは、選択した画像のサイズを指定する方法を提供し ます。オプションを表示するには、ここをクリックしてください

2.元のサイズを知る必要があり、特定したくない場合は、以下のコードを試してみてください。

function got_image(image_uri) {
    window.resolveLocalFileSystemURI(image_uri, function(fileEntry) {
       fileEntry.file(function (fileObj) {
                    var fileName = fileObj.fullPath;
                    var originalLogo = new Image();
                    originalLogo.src =fileName;
                    //get the size by: originalLogo.width,originalLogo.height
                });
    })
}
于 2013-03-01T02:17:11.317 に答える
0

これを行う 1 つの方法は、Capture API から MediaFile を使用することです。

var mediaFile = new MediaFile("myfile.jpg", "/path/to/myfile.jpg");
mediaFile.getFormatData(function(data) {
    console.log("width: " data.width);
    console.log("height: " data.height);
});

現在受け入れられているソリューションよりもはるかに少ないコードにする必要があります。

于 2013-03-10T02:55:54.963 に答える