J2ME中的Image.createImage问题

发布于 2024-07-08 17:05:02 字数 269 浏览 7 评论 0原文

我在 J2ME 上尝试了这个,

try {
    Image immutableThumb = Image.createImage( temp, 0, temp.length);
} catch (Exception ex) {
    System.out.println(ex);
}

但遇到了这个错误: java.lang.IllegalArgumentException:

我该如何解决这个问题?

I tried this on J2ME

try {
    Image immutableThumb = Image.createImage( temp, 0, temp.length);
} catch (Exception ex) {
    System.out.println(ex);
}

I hit this error:
java.lang.IllegalArgumentException:

How do I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

£烟消云散 2024-07-15 17:05:03

如果第一个参数的格式不正确或无法解码,Image.createImage() 会抛出 IllegalArgumentException。 (我假设 temp 是一个 byte[])。

http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Image.html#createImage(byte[],%20int,%20int)

(此网址由于某种原因拒绝成为超链接(?))

Image.createImage() throws an IllegalArgumentException if the first argument is incorrectly formatted or otherwise cannot be decoded. (I'm assuming that temp is a byte[]).

http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Image.html#createImage(byte[],%20int,%20int)

(This URL refuses to become a hyperlink for some reason (?))

給妳壹絲溫柔 2024-07-15 17:05:03

如果没有更多细节或更多周围代码,很难说,但我最初怀疑您尝试加载的文件采用了设备不支持的格式。

It's hard to say without more details or more surrounding code, but my initial suspicion is that the file your are trying to load is in a format not supported by the device.

心不设防 2024-07-15 17:05:03

让我们看一下文档:抛出 IllegalArgumentException

如果 imageData 格式不正确或无法解码

则可能的原因可能是图像格式不受支持或数据被截断。 请记住,您应该将整个文件传递给该方法,包括所有标头。 如果你对格式有疑问,最好选择PNG,无论如何它都必须支持。

Let us have a look at the docs: IllegalArgumentException is thrown

if imageData is incorrectly formatted or otherwise cannot be decoded

So the possible reason can be either unsupported format of the image, or truncated data. Remember, you should pass entire file to that method, including all the headers. If you have doubts about the format, you'd better choose PNG, it must be supported anyway.

一城柳絮吹成雪 2024-07-15 17:05:03

我的 MIDLET 也遇到了同样的问题,我的例子中的问题是我从套接字的 InputStream 读取的 JPEG 图像附带的 HTTP 标头。 我通过在字节数组中找到由两个字节标识的 JPEG SOI 标记来解决这个问题:FFD8。 然后,当我在字节数组中找到 FFD8 的位置时,我会修剪表示 HTTP 标头的起始字节,然后我可以调用 createImage() 而不会出现任何异常被扔...

你应该检查一下你是否遇到这种情况。 只需检查 (temp[0] == 0xFF && temp[1] == 0xD8) 是否为真,如果不是,则修剪 temp 的开头所以你删除 HTTP 标头或其他一些垃圾...

PS
我假设您正在读取 JPEG 图像,如果不是,请在 temp 数组中查找适当的标头。

另外,如果这没有帮助,并且您正在读取 JPEG 图像,请确保数组以 FFD8 开头并以 FFD9 结尾(这是 EOI 标记)。 如果它没有以 EOI 结尾,只需修剪结尾,就像我为 SOI 解释的那样......

PPS
如果您发现 temp 中的数据有效,则说明您的平台无法解码 JPEG 图像,或者 temp 中的图像对于 JPEG 解码器来说太大。

I just had the same problem with my MIDLET and the problem in my case was the HTTP header that comes along the JPEG image that I read from the socket's InputStream. And I solved it by finding the JPEG SOI marker that is identified by two bytes: FFD8 in my byte array. Then when I find the location of the FFD8 in my byte array, I trim the starting bytes that represent the HTTP header, and then I could call createImage() without any Exception being thrown...

You should check if this is the case with you. Just check is this true (temp[0] == 0xFF && temp[1] == 0xD8) and if it is not, trim the start of temp so you remove HTTP header or some other junk...

P.S.
I presume that you are reading JPEG image, if not, look for the appropriate header in the temp array.

Also if this doesn't help, and you are reading JPEG image make sure that the array starts with FFD8 and ends with FFD9 (which is the EOI marker). And if it doesn't end with the EOI just trim the end like I explained for SOI...

P.P.S
And if you find that the data in temp is valid, then your platform cannot decode the JPEG images or the image in temp is to large for JPEG decoder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文