java 手机号压缩,Java-Android-在一部手机上压缩位图的速度非...

我正在编写一个具有简单摄像头功能的应用程序.我在主要活动中拍照,然后启动一个新线程以旋转图像,添加透明水印并将其另存为.PNG.

这个程序在运行自定义rom的三星银河S3上运行良好:AOKP,Android 4.4.2.下面给出的代码执行总时间约为2秒.根据任务管理器,我的应用程序使用了大约9 Mb的内存.

当我在运行有库存(有根)的Android 4.4.2的银河S4上运行该应用程序时,执行时间超过了27秒.该应用程序根据任务管理器使用大约124 Mb的内存(太多).有时,应用程序崩溃并抛出OutOfMemory错误,这在S3上不会发生.

这是负责将图像保存到我的SaveThread中的代码:

TimingLogger tl = new TimingLogger("MyTag", "Saving");

String name = new SimpleDateFormat("dd-MM-yyyy_HH-mm-ss")

.format(new Date());

File myNewFolder = new File(path);

myNewFolder.mkdir();

File file = new File(path, name + ".JPEG");

OutputStream imageFileOS;

// Get EXIF rotation from byte array

int rotation = getOrientation(data);

tl.addSplit("Rotation calculated");

// Convert byte array to bitmap, and watermark bitmap

Bitmap pictureObject = BitmapFactory.decodeByteArray(data, 0,

data.length);

tl.addSplit("Decoded ByteArray to Bitmap");

// Rotate bitmap image according to EXIF data

pictureObject = rotateBitmap(rotation, pictureObject);

tl.addSplit("Bitmap rotated");

// Add watermark to rotated bitmap

Bitmap waterMarkedBitmap = addWaterMarkText(pictureObject);

tl.addSplit("Bitmap watermarked");

try {

imageFileOS = new FileOutputStream(file);

waterMarkedBitmap.compress(Bitmap.CompressFormat.PNG, 100,

imageFileOS);

tl.addSplit("Bitmap compressed");

imageFileOS.flush();

imageFileOS.close();

} catch (Exception e) {

e.printStackTrace();

}

如您所见,我添加了一个计时记录器来找出花费这么长时间的时间.下面的LogCat返回结果表明,将位图压缩为.PNG需要很长时间.我认为这可能与垃圾收集器有关.我的Logcat在执行期间返回以下内容:

02-04 13:57:08.166: I/Choreographer(30003): Skipped 73 frames! The

application may be doing too much work on its main thread.

02-04 13:57:10.528: D/dalvikvm(30003): GC_FOR_ALLOC freed 552K, 19% free 24803K/30324K, paused 48ms, total 49ms

02-04 13:57:10.628: I/dalvikvm-heap(30003): Grow heap (frag case) to 76.336MB for 51121168-byte allocation

02-04 13:57:10.918: D/dalvikvm(30003): GC_FOR_ALLOC freed 4K, 7% free 74723K/80248K, paused 23ms, total 23ms

02-04 13:57:11.019: I/dalvikvm-heap(30003): Grow heap (frag case) to 125.086MB for 51121168-byte allocation

02-04 13:57:11.859: D/dalvikvm(30003): GC_FOR_ALLOC freed 49923K, 43% free 74723K/130172K, paused 25ms, total 26ms

02-04 13:57:11.869: I/dalvikvm-heap(30003): Grow heap (frag case) to 125.086MB for 51121168-byte allocation

02-04 13:57:39.967: D/MyTag(30003): Saving: begin

02-04 13:57:39.967: D/MyTag(30003): Saving: 3 ms, Rotation calculated

02-04 13:57:39.967: D/MyTag(30003): Saving: 418 ms, Decoded ByteArray to Bitmap

02-04 13:57:39.967: D/MediaScannerConnection(30003): Scanning file 7klwibgf7fvntblfd7(7KhzCbvfib7(,6()6)(*._*+6./6*(5QHFG

02-04 13:57:39.967: D/MyTag(30003): Saving: 939 ms, Bitmap rotated

02-04 13:57:39.967: D/MyTag(30003): Saving: 96 ms, Bitmap watermarked

02-04 13:57:39.967: D/MyTag(30003): Saving: 28023 ms, Bitmap compressed

02-04 13:57:39.967: D/MyTag(30003): Saving: 0 ms, Done saving

02-04 13:57:39.967: D/MyTag(30003): Saving: 8 ms, Media scanned

02-04 13:57:39.967: D/MyTag(30003): Saving: end, 29487 ms

我不确定是否相关,但LogCat行:

D/MediaScannerConnection(30003): Scanning file 7klwibgf7fvntblfd7(7KhzCbvfib7(,6()6)(*._*+6./6*(5QHFG

显然不会显示扫描文件的路径(尽管它确实可以工作).当我将S3连接到Eclipse时,此行会显示扫描的文件路径.

我尝试更改压缩质量,但是并没有在速度或内存使用方面做出重大改变.我不知道是什么原因导致了RAM使用率和执行时间的巨大差异.是什么会导致这种差异,我该如何解决?

编辑:完整地说,这就是在银河S3上压缩的速度:

02-04 15:40:47.980: D/MyTag(25536): Saving: 6 ms, Rotation calculated

02-04 15:40:47.980: D/MyTag(25536): Saving: 53 ms, Decoded ByteArray to Bitmap

02-04 15:40:47.980: D/MyTag(25536): Saving: 94 ms, Bitmap rotated

02-04 15:40:47.980: D/MyTag(25536): Saving: 19 ms, Bitmap watermarked

02-04 15:40:47.980: D/MyTag(25536): Saving: 210 ms, Bitmap compressed

02-04 15:40:47.980: D/MyTag(25536): Saving: 0 ms, Done saving

02-04 15:40:47.980: D/MyTag(25536): Saving: 3 ms, Media scanned

02-04 15:40:47.980: D/MyTag(25536): Saving: end, 385 ms

当我在S3上使用前置摄像头拍摄照片时,压缩时间要长得多(我真的不知道为什么…):

02-04 20:24:31.245: D/MyTag(29496): Saving: begin

02-04 20:24:31.245: D/MyTag(29496): Saving: 2 ms, Rotation calculated

02-04 20:24:31.245: D/MyTag(29496): Saving: 95 ms, Decoded ByteArray to Bitmap

02-04 20:24:31.245: D/MyTag(29496): Saving: 160 ms, Bitmap rotated

02-04 20:24:31.245: D/MyTag(29496): Saving: 25 ms, Bitmap watermarked

02-04 20:24:31.245: D/MyTag(29496): Saving: 4548 ms, Bitmap compressed

02-04 20:24:31.245: D/MyTag(29496): Saving: 0 ms, Done saving

02-04 20:24:31.245: D/MyTag(29496): Saving: 3 ms, Media scanned

02-04 20:24:31.245: D/MyTag(29496): Saving: end, 4833 ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值