简体   繁体   English

如何从J2ME中的大图像文件中获取经验?

[英]How to make a thumb from a big image file in J2ME?

I'm trying to display a big image file in a J2ME application. 我正在尝试在J2ME应用程序中显示大图像文件。 But I found that when the image file is too big I can not even create the Image instance and get a OutOfMemory exception. 但是我发现当图像文件太大时,我什至无法创建Image实例并获得OutOfMemory异常。

I suppose I can read the image file in small chunks and create a thumbnail to show to the user? 我想我可以一小块读取图像文件并创建缩略图以显示给用户?
Is there a way to do this? 有没有办法做到这一点? Or is there any other method to display the image file in the application? 还是有其他方法可以在应用程序中显示图像文件?
Thanks. 谢谢。

There are a number of things to try, depending on exactly what you are trying to do and what handset you want your application to run on. 您可以尝试多种方法,具体取决于您要执行的操作以及希望在其上运行应用程序的手机。

If your image is packaged inside your MIDlet JAR file, You have less control over what the MIDP runtime does because the data needs to be unzipped before it can be loaded as an Image. 如果将映像打包在MIDlet JAR文件中,则您对MIDP运行时的操作控制较少,因为在将数据作为映像加载之前需要解压缩数据。 In that case, I would suggest simply packaging a smaller image. 在这种情况下,我建议您仅打包一个较小的图像。 Either reduce the number of pixels or the number of bytes used to encode each pixel. 减少像素数或减少用于编码每个像素的字节数。

If you can read the image bytes from a GCF-based InputStream (file, network...), You need to understand the image format (BMP is straightforward, JPEG less so...) so you can scale it down into a mutable Image object that would take less memory, one chunk at a time. 如果您可以从基于GCF的InputStream(文件,网络...)读取图像字节,则需要了解图像格式(BMP很简单,而JPEG则更少...),以便可以将其缩小为可变的占用较少内存的Image对象,一次只占用一个块。

In that case, you also need to decide what your scaling algorithm should be. 在这种情况下,您还需要确定缩放算法应该是什么。 Turning 32 bits pixels in a file into 8 bits pixels in memory might not actually work as expected if the LCDUI implementation on your mobile phone was badly written. 如果手机上的LCDUI实现编写不正确,则将文件中的32位像素转换为内存中的8位像素实际上可能无法按预期方式工作。

Depending on the content of the image, simply removing half of the pixel columns and half of the pixel lines may be either exactly what you need or way too naive an approach. 根据图像的内容,仅删除一半的像素列和一半的像素行可能恰好是您所需要的,或者是太幼稚的方法。 You may want to look at existing image scaling algorithms and write one into your application. 您可能需要查看现有的图像缩放算法,然后将其写入应用程序。

Remember that basic LCDUI may not be the only way to display an image on the screen. 请记住,基本的LCDUI可能不是在屏幕上显示图像的唯一方法。 JSR-184, JSR-239, JSR-226 and eSWT could all allow you to do that in a way that may be totally independant from your handset LCDUI implementation. JSR-184,JSR-239,JSR-226和eSWT都可以使您以完全独立于手机LCDUI实施方式的方式进行操作。

Finally, let's face it, if your phone MIDP runtime doesn't allow you to create at least 2 images the size of your screen at full color depth at the same time, then it might be time to decide to not support that specific handset. 最后,让我们面对现实吧,如果您的手机MIDP运行时不允许您同时在全色深度上创建至少两张屏幕大小的图像,那么可能是时候决定不支持该特定手机了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM