将ActionScript JPG字节数组传递给Javascript(最终传递给PHP) [英] Passing an ActionScript JPG Byte Array to Javascript (and eventually to PHP)

查看:135
本文介绍了将ActionScript JPG字节数组传递给Javascript(最终传递给PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的Web应用程序具有一项功能,该功能使用Flash(AS3)通过用户的网络摄像头拍照,然后将所得的字节数组传递给PHP,在此处将其重建并保存在服务器上.

Our web application has a feature which uses Flash (AS3) to take photos using the user's web cam, then passes the resulting byte array to PHP where it is reconstructed and saved on the server.

但是,我们需要能够使该Web应用程序脱机,并且我们选择了Gears来做到这一点.用户使应用程序脱机,执行任务,然后在重新连接到服务器时,我们将数据同步"回我们的中央数据库.

However, we need to be able to take this web application offline, and we have chosen Gears to do so. The user takes the app offline, performs his tasks, then when he's reconnected to the server, we "sync" the data back with our central database.

我们不再具有PHP与Flash交互的功能,但是我们仍然需要允许用户拍摄和保存照片.我们不知道如何将Flash创建的JPG保存在本地数据库中.我们的希望是,我们可以保存字节数组,序列化的字符串,或者以某种方式实际保留对象本身,然后将其传递回PHP或Flash(然后是PHP)以重新创建JPG.

We don't have PHP to interact with Flash anymore, but we still need to allow users to take and save photos. We don't know how to save a JPG that Flash creates in a local database. Our hope was that we could save the byte array, a serialized string, or somehow actually persist the object itself, then pass it back to either PHP or Flash (and then PHP) to recreate the JPG.

我们尝试过: -将字节数组传递给Javascript而不是PHP,但是javascript似乎无法对其进行任何处理(该对象似乎被剥夺了其方法) -在Flash中将字节数组字符串化,然后将其传递给Javascript,但是我们总是得到相同的字符串:

We have tried: - passing the byte array to Javascript instead of PHP, but javascript doesn't seem to be able to do anything with it (the object seems to be stripped of its methods) - stringifying the byte array in Flash, and then passing it to Javascript, but we always get the same string:

ÿØÿà

现在,我们正在考虑在Flash中对字符串进行序列化,将其传递给Javascript,然后在返回路径上,将该字符串传递回Flash,然后将其传递给PHP以重新构造为JPG. (he).由于我们团队中没有人具有广泛的Flash背景,因此我们有些失落.

Now we are thinking of serializing the string in Flash, passing it to Javascript, then on the return route, passing that string back to Flash which will then pass it to PHP to be reconstructed as a JPG. (whew). Since no one on our team has extensive Flash background, we're a bit lost.

序列化是要走的路吗?有没有更现实的方法可以做到这一点?有人对这种事情有经验吗?也许我们可以构建一个与AS中的字节数组类相同的javascript类?

Is serialization the way to go? Is there a more realistic way to do this? Does anyone have any experience with this sort of thing? Perhaps we can build a javascript class that is the same as the byte array class in AS?

推荐答案

我不确定您为什么要在这里使用Javascript.无论如何,您粘贴的字符串看起来像是JPG标头的开头.问题是JPG肯定会包含NUL(值为0的字符).这很可能会截断字符串(就像您发布的示例一样).如果要字符串化" JPG,则标准方法是将其编码为Base 64.

I'm not sure why you would want to use Javascript here. Anyway, the string you pasted looks like the beginning of a JPG header. The problem is that a JPG will for sure contain NULs (characters with 0 as its value). This will most likely truncate the string (as it seems to be the case with the sample you posted). If you want to "stringify" the JPG, the standard approach is encoding it as Base 64.

但是,如果要在本地保留数据,则可以在Flash中进行处理.这很简单,但是有一些限制.

If you want to persist data locally, however, there's a way to do it in Flash. It's simple, but it has some limitations.

您可以使用本地共享对象为此.默认情况下,限制为100 Kb,这对于图像文件来说是远远不够的.不过,您可以要求用户为您的应用分配更多空间.无论如何,我都会尝试将图像存储为JPG而不是原始像素,因为大小上的差异非常明显.

You can use a local Shared Object for this. By default, there's a 100 Kb limit, which is rather inadequate for image files; you could ask the user to allot more space to your app, though. In any case, I'd try to store the image as JPG, not the raw pixels, since the difference in size is very significative.

Shared Objects将透明地为您处理序列化/反序列化.有一些警告:并非每个对象都可以真正被序列化;对于初学者,它必须具有无参数的构造函数;诸如Sprites,MovieClips等的DisplayObject将无法正常工作.但是,可以序列化ByteArray,因此可以将JPG保存在本地(如果用户允许额外的空间).您应该使用AMF3作为编码方案(我认为这是默认设置).另外,您还应该使用 registerClassAlias 保留序列化对象的类型(否则将被视为Object对象).您仅需在应用程序生命周期中执行一次此操作,但是必须在对共享对象进行任何读/写操作之前执行此操作.

Shared Objects will handle serialization / deserialization for you transparently. There are some caveats: not every object can really be serialized; for starters, it has to have a parameterless constructor; DisplayObjects such as Sprites, MovieClips, etc, won't work. It's possible to serialize a ByteArray, however, so you could save your JPGs locally (if the user allows for the extra space). You should use AMF3 as the encoding scheme (which is the default, I think); also, you should map the class you're serializing with registerClassAlias to preserve the type of serialized the object (otherwise it will be treated as an Object object). You only need to do it once in the app life cycle, but it must be done before any read / write to the Shared Object.

类似的东西:

registerClassAlias("flash.utils.ByteArray",ByteArray);

registerClassAlias("flash.utils.ByteArray",ByteArray);

我会使用共享对象而不是Javascript.请记住,您很可能必须要求用户为您提供更多存储图像的空间(如果您允许它们脱机工作,这似乎足够合理),并且用户可以在任何时候删除数据时间(就像他可以删除其浏览器的Cookie一样).

I'd use Shared Objects rather than Javascript. Just keep in mind that you'll most likely have to ask the user to give you more space for storing the images (which seems reasonable enough if you're allowing them to work offline), and that the user could delete the data at any time (just like he could delete their browser's cookies).

修改

我意识到我对您提出的问题的我们选择Gears来做"这一部分并没有给予太多关注.

I realize I didn't really pay much attention the "we have chosen Gears to do so" part of your question.

在这种情况下,您可以尝试使用base 64方法将数据传递给JS.从Actionscript方面来说,这很容易(从那里获得许多可用的Base64编码器/解码器之一),并且我认为Gear的API必须已经有可用的编码器/解码器(或者至少应该不难找到一种编码器/解码器).届时,您可能必须将其转换为Blob并将其存储到磁盘(也许使用

In that case, you could give the base 64 approach a try to pass the data to JS. From the Actionscript side it's easy (grab one of the many available Base64 encoders/decoders out there), and I assume the Gear's API must have an encoder / decoder available already (or at least it shouldn't be hard to find one). At that point you'll probably have to turn that into a Blob and store it to disk (maybe using the BlobAPI, but I'm not sure as I don't have experience with Gears).

这篇关于将ActionScript JPG字节数组传递给Javascript(最终传递给PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆