在 Android 中查看大图像的一小部分区域
总而言之,这就是我想要做的:
我想将 5mega 像素图像的 100x100 区域(任何部分)加载到 Android Bitmap 类中,以便我可以将其绘制到画布元素上 就这么简单。这就是我想做的。就是这样。而已。一点也没有少。听起来很简单。所以,在你脸上露出傻笑之前,请继续往下读。
据我所知,这个问题已经被问过一百万次了。我也做了功课,进行了研究。不幸的是,我从各个方面都陷入了死胡同。也许我需要把我的问题说得足够清楚。
Android VM 允许您分配的堆数量是有限制的。因此,使用以下函数无法加载大位图,即使是来自其自己的相机(我拥有一台 Nexus S)的位图。
BitmapFactory.decodeStream(InputStream is)
是的,我可以使用 BitmapFactory.Options 来缩放它,但是我应该如何放大呢?
现在我正在尝试设计一个可以平滑放大/缩小图像的图像查看器。显然,如果我什至无法加载图像,这是不可能的。
在姜饼中,我们有一个新类 BitmapRegionDecoder ,但我正在为 Froyo 设计我的应用程序。所有这些类都与使用 Skia 2D 图形库的本机 API 挂钩。 Android NDK 也不允许访问这些 api。也许有一种方法可以手动构建 Skia 并使用它来加载 java 位图。但我不确定如何?
它似乎已经解决了(http: //blog.javia.org/how-to-work-around-androids-24-mb-memory-limit/)在此应用程序中 - https://market.android.com/details?id=image.viewer 通过使用 malloc 在本机 C 代码中分配的内存/新。但我不知道怎么办?
因此,让事情变得更清楚,我想要的是可以将图像放大/缩小到全分辨率的东西。如果可能的话,它应该是平滑的,或者至少可以在单独的线程中完成。
如果我需要使用 OpenGL,那么也请给我一个示例代码。
In a gist this is what I want to do:
I want to load a 100x100 region (any part) of a 5mega pixel Image into a Android Bitmap class so that I can draw it onto a canvas element
Its that simple. Thats all I want to do. Thats it. Nothing more. Nothing less. Sounds simple enough. So before you have a smirk on your face, read on further down.
I understand that this question has been asked a million times already. And I have also done my homework, researching it. Unfortunately I have hit a dead end from all sides. Maybe I need to make my question clear enough.
There is a limit on the amount of heap the Android VM lets you allocate. So loading a large bitmap, even the one from its own camera (I own a Nexus S) is not possible by using the following function.
BitmapFactory.decodeStream(InputStream is)
Yes I could scale it by using BitmapFactory.Options, but how then should I zoom in?
Now I am trying to design an image viewer which can smoothly zoom in/out from the image. Obviously that's not possible if I can't even load the image.
In gingerbread we have a new class BitmapRegionDecoder , but I am designing my app for Froyo. All these classes have hooks into the native api which use the Skia 2D Graphics Library. The Android NDK does not give access to these api's also. Maybe there is a way to manually build Skia and use it to load a java bitmap. But I am not sure how?
It seems to have been solved already (http://blog.javia.org/how-to-work-around-androids-24-mb-memory-limit/) in this app - https://market.android.com/details?id=image.viewer by using the memory allocated in the native C code using malloc/new. But I cant figure out how?
So make things clearer what I want is something that can zoom in/out of an image to full resolution. If possible it should be smooth or atleast it can be done in a separate thread.
If I need to use OpenGL, then please give me a sample code also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己想出了这个办法。 :)
这不是最好的方法,但效果很好。
我用本机代码创建了位图,并使用 Skia 图形库访问其区域的子集。 (这意味着我正在访问私有 API,并且其功能很容易被较新的 Android 版本破坏)。
http://code.google.com/p/skia/
我必须链接到此我的 C++ 代码中包含 lib 并通过 JNI 访问此功能。
I figured this out on my own. :)
Its not the best way, but it works wonderfully.
I created the bitmap in native code and accessed subset of its region using the Skia graphics library. (Which would mean that I am accessing private API's and its functionality could easily break with newer Android Versions).
http://code.google.com/p/skia/
I had to link against this lib in my C++ code and access this functionality through JNI.