开发者

JSON bitmap data

开发者 https://www.devze.com 2022-12-30 21:42 出处:网络
Is it possible, or reasonable, to encode bitmap data into JSON to be returned in a webservice? Update:Yes, this worked better than I thought.I made a .NET composite object for a combination of images

Is it possible, or reasonable, to encode bitmap data into JSON to be returned in a webservice?

Update: Yes, this worked better than I thought. I made a .NET composite object for a combination of images together with image data

Public Class AllThumbnails Public imgAllThumbs As String Public positions() As Drawing.Rectangle End Class

and accessed it via jQuery AJAX thusly:

$.ajax({
    type: "POST",
    url: "WebService.asmx/makeAllThumbnailsImage",
    data: "{DocumentNumber : \"" + DocumentNumber + "\"} ",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        var adl = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;

        var data = Base64.decode(adl.imgAllThumbs);
        $('#output').append("<p><strong>" + data.length + "</strong></p>");
        $('#output').ap开发者_JS百科pend("<p><strong><i>" + adl.positions.length + "<i></strong></p>");

    },
    failure: function (msg) {
        $('#output').text(msg);
    }
});

I did have to increase a value in my web.config since my image data was overrunning the standard jsonSerialization buffer:

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="262144">
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>

Thanks guys for your help.


A bitmap is binary data. JSON is to be represented as character data. So you need to convert binary data to character data and vice versa without loss of information. A commonly used encoding for this is Base64. It's unclear which programming language you're targeting on, so I can't give a more detailed answer, but almost all self-respected languages have either a builtin Base64 encoder or a 3rd party library which can do that. PHP for example has base64_encode() and base64_decode(). Java has Apache Commons Codec Base64. For JavaScript there is this example. And so on.


Sounds like Binary JSON.

Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. Like JSON, BSON sup­ports the em­bed­ding of doc­u­ments and ar­rays with­in oth­er doc­u­ments and ar­rays. BSON also con­tains ex­ten­sions that al­low rep­res­ent­a­tion of data types that are not part of the JSON spec. For ex­ample, BSON has a Date type and a BinData type.

BSON can be com­pared to bin­ary inter­change for­mats, like Proto­col Buf­fers. BSON is more "schema-less" than Proto­col Buf­fers, which can give it an ad­vant­age in flex­ib­il­ity but also a slight dis­ad­vant­age in space ef­fi­ciency (BSON has over­head for field names with­in the seri­al­ized data).


You might be able to use a data uri (http://en.wikipedia.org/wiki/Data_URI_scheme) to decode encoded bitmap data... would be interesting to try :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消