android处理图片的一些问题总结

第一个问题是out of memory

1java.lang.OutOfMemoryError: bitmap size exceeds VM budget

这个据说是VM对一个程序申请的所有的bitmap对象会有一个最大值的要求。解决这个问题有几个方法:

1. 从源文件生成图片时,直接将图片缩小,而不是加载原始大小的图片。如下代码:

01Bitmap bitmap = null;
02if (mUseZoomOut || mUseZoomIn) {
03    // decode image size (decode metadata only, not the whole image)
04    o = new BitmapFactory.Options();
05    o.inJustDecodeBounds = true;
06    stream = new FileInputStream(filename);
07    BitmapFactory.decodeStream(stream, null, o);
08    stream.close();
09 
10    // get original image size
11    int inWidth =  o.outWidth;
12    int inHeight = o.outHeight;
13    clog(String.format("Original bitmap size: (%dx%d).", inWidth, inHeight));
14 
15    // get size for pre-resized image
16    o = new Options();
17    o.inSampleSize = Math.max(inWidth/targetWidth, inHeight/targetHeight);
18}
19 
20// decode pre-resized image
21stream = new FileInputStream(filename);
22// o.inPurgeable = true;
23bitmap = BitmapFactory.decodeStream(stream, null, o);
24stream.close();
25clog(String.format("Pre-sized bitmap size: (%dx%d).", bitmap.getWidth(), bitmap.getHeight()));

2. 及时删除不需要使用的bitmap对象,不要将所有的对象都cache住

3. 增加程序的heap size。从某个版本开始,android manifest文件里有一个新的属性了:

1android:largeHeap="true"

android:largeHeap

1Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results.
2Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
3To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().

第二个问题是

1Bitmap too large to be uploaded into a texture exception

这个问题下面链接有详细描述:

http://stackoverflow.com/questions/7428996/hw-accelerated-activity-how-to-get-opengl-texture-size-limit

简单说就是硬件加速的时候,对图片的大小有限制。不同设备可能有不同的最大值。这个问题悲催的地方是,程序貌似没有捕获到这个exception, 结果是程序也不报错,图片也显示不出来。只有看debug log才能发现这个error message.

一个解决的方法是禁止硬件加速,简单粗暴:

1android:hardwareAccelerated="false"
2 
3android:hardwareAccelerated
4Whether or not hardware-accelerated rendering should be enabled for all activities and views in this application — "true" if it should be enabled, and "false" if not. The default value is "true" if you've set either minSdkVersion or targetSdkVersion to "14" or higher; otherwise, it's "false".
5Starting from Android 3.0 (API level 11), a hardware-accelerated OpenGL renderer is available to applications, to improve performance for many common 2D graphics operations. When the hardware-accelerated renderer is enabled, most operations in Canvas, Paint, Xfermode, ColorFilter, Shader, and Camera are accelerated. This results in smoother animations, smoother scrolling, and improved responsiveness overall, even for applications that do not explicitly make use the framework's OpenGL libraries.
6Note that not all of the OpenGL 2D operations are accelerated. If you enable the hardware-accelerated renderer, test your application to ensure that it can make use of the renderer without errors.
7For more information, read the Hardware Acceleration guide.

比较好的解决方法是类似google map的实现:将图片分成不同的块,每次加载需要的块。android提供了一个方法:

http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html

1public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)
2 
3public Bitmap decodeRegion (Rect rect, BitmapFactory.Options options)

采取上述操作后,就可以加载很多图片,同时也可以显示超级大图了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值