我正在构建一个缓冲 N 相机帧的应用程序,当用户点击一个按钮时,它会使用应用效果的所有已保存帧来保存照片。
我在 AsyncTask 上保存照片并处理帧。当我执行它时,我从屏幕上删除了所有内容,只留下一个 TextView 来显示保存照片的进度。
目前 AsyncTask doInBackground 看起来像这样:
protected Void doInBackground(Integer... params)
{
int w = mBuffer.get(0).getWidth();
int h = mBuffer.get(0).getHeight();
int lineHeight = h / mBuffer.size();
int currentHeight = 0;
Log.d("output", "saving photo "+w+", "+h);
for (int i = 0; i < mBuffer.size(); i++)
{
YuvImage image = mBuffer.get(i);
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (!image.compressToJpeg(new Rect(0, currentHeight, w, lineHeight), 50, out))
{
Log.d("output", "problem converting yuv to jpg");
break;
}
currentHeight += lineHeight;
Log.d("output", "currentHeight: "+currentHeight);
publishProgress((int)((i / (float)mBuffer.size()) * 100));
try
{
out.flush();
out.close();
out = null;
}
catch(Exception e)
{
}
System.gc();
try
{
Thread.sleep(200);
}
catch(Exception e)
{
}
if (isCancelled())
{
break;
}
}
return null;
}
我已经为这个问题删除了很多不太重要的代码部分,这只是引发 OutOfMemory 问题的部分。基本上我在那里做的是从图像中获取一行并将其压缩为 JPEG。但它只能压缩第一张图片。当它转到第二个时,它会引发一个 OutOfMemory 异常。
System.GC(), Thread.sleep(), out.flush(), out.close( ),都是解决问题的不成功尝试。
当前mBuffer的大小是5,最初是32。从DDMS Heap调试来看,它的Heap Size是30MB,正在分配9MB。显然还有很大的成长空间。如果我删除 compressToJpeg(),AsyncTask 就可以正常完成。
有人能解决这个问题吗?我应该尝试自己的 YUV -> JPEG 转换器吗?
使用最新代码进行编辑:
protected Void doInBackground(Integer... params)
{
int lineHeight = mHeight / mBufferSize;
int currentHeight = 0;
Log.d("output", "saving photo: "+mWidth+", "+mHeight);
File outputPath = new File(Environment.getExternalStorageDirectory().getPath() + "/camerafluid-output");
outputPath.mkdirs();
for (int i = 0; i < mBufferSize; i++)
{
File imageFile = new File(mCachePath, "image-"+0);
File outputFile = new File(outputPath, "image-"+0);
if (!imageFile.exists())
{
Log.d("output", "image "+i+" not found on cache directory");
continue;
}
try
{
int size = (int)imageFile.length();
byte[] imageBytes = new byte[size];
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(imageFile));
buf.read(imageBytes, 0, size);
buf.close();
YuvImage image = new YuvImage(imageBytes, ImageFormat.NV21, mWidth, mHeight, null);
imageBytes = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (!image.compressToJpeg(new Rect(0, currentHeight, mWidth, lineHeight), 50, out))
{
Log.d("output", "problem converting yuv to jpg");
break;
}
FileOutputStream s = new FileOutputStream(outputFile);
s.write(out.toByteArray());
s.flush();
s.close();
s = null;
currentHeight += lineHeight;
Log.d("output", "currentHeight: "+currentHeight);
publishProgress((int)((i / (float)mBufferSize) * 100));
System.gc();
}
catch(Exception e)
{
}
if (isCancelled())
{
break;
}
}
return null;
}
我正确地保存了图像,因为我可以在缓存目录中看到保存的文件,这是保存缓冲区并执行 AsyncTask 的 Activity 部分:
public void takePhoto()
{
Log.d("output", "taking photo");
//vfranchi - save the buffer on the disk to free memory
File cachePath = new File(getExternalCacheDir().getPath() + "/buffer");
if (!cachePath.exists())
{
Log.d("output", "cache path doesnt exist");
cachePath.mkdirs();
}
int count = 0;
int width = mPhotoBuffer.get(0).getWidth();
int height = mPhotoBuffer.get(0).getHeight();
int bufferSize = mPhotoBuffer.size();
for(YuvImage i : mPhotoBuffer)
{
File f = new File(cachePath, "image-"+count);
try
{
FileOutputStream s = new FileOutputStream(f);
s.write(i.getYuvData());
s.flush();
s.close();
Log.d("output", "saved image "+count);
count++;
}
catch(Exception e)
{
Log.d("output", "unable to save image "+count+"\n"+e.getLocalizedMessage());
}
}
mPhotoBuffer = null;
System.gc();
SavePhotoTask task = new SavePhotoTask(this, cachePath, width, height, bufferSize);
task.execute(0);
}
Logcat 开始拍照方法:
03-30 12:48:08.534: D/output(15359): taking photo
03-30 12:48:09.011: V/Camera-JNI(15359): setHasPreviewCallback: installed:0, manualBuffer:0
03-30 12:48:09.011: V/Camera-JNI(15359): get_native_camera: context=0x138520, camera=0x1eea78
03-30 12:48:09.011: V/Camera-JNI(15359): Clearing callback buffers, 0 remained
03-30 12:48:09.011: V/Camera-JNI(15359): stopPreview
03-30 12:48:09.011: V/Camera-JNI(15359): get_native_camera: context=0x138520, camera=0x1eea78
03-30 12:48:15.323: D/output(15359): saved image 0
03-30 12:48:16.628: D/output(15359): saved image 1
03-30 12:48:17.880: D/output(15359): saved image 2
03-30 12:48:19.073: D/output(15359): saved image 3
03-30 12:48:20.545: D/output(15359): saved image 4
03-30 12:48:28.089: D/output(15359): saving photo: 848, 480
03-30 12:48:33.276: D/skia(15359): onFlyCompress
03-30 12:48:35.020: D/output(15359): currentHeight: 96
03-30 12:48:44.144: D/output(15359): currentHeight: 192
03-30 12:48:52.581: D/skia(15359): onFlyCompress
03-30 12:48:52.878: I/dalvikvm-heap(15359): Grow heap (frag case) to 14.975MB for 2095120-byte allocation
03-30 12:48:53.112: I/dalvikvm-heap(15359): Grow heap (frag case) to 17.975MB for 4192272-byte allocation
03-30 12:48:53.566: I/dalvikvm-heap(15359): Grow heap (frag case) to 23.975MB for 8386576-byte allocation
03-30 12:48:54.855: I/dalvikvm-heap(15359): Grow heap (frag case) to 35.975MB for 16775184-byte allocation
03-30 12:48:56.620: I/dalvikvm-heap(15359): Forcing collection of SoftReferences for 33552400-byte allocation
03-30 12:48:56.644: E/dalvikvm-heap(15359): Out of memory on a 33552400-byte allocation.
03-30 12:48:56.644: I/dalvikvm(15359): "AsyncTask #1" prio=5 tid=12 RUNNABLE
03-30 12:48:56.644: I/dalvikvm(15359): | group="main" sCount=0 dsCount=0 obj=0x40d71628 self=0x22eea8
03-30 12:48:56.644: I/dalvikvm(15359): | sysTid=15404 nice=10 sched=0/0 cgrp=bg_non_interactive handle=2056240
03-30 12:48:56.651: I/dalvikvm(15359): | schedstat=( 3387725830 799926751 876 ) utm=327 stm=10 core=0
03-30 12:48:56.651: I/dalvikvm(15359): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~91)
03-30 12:48:56.651: I/dalvikvm(15359): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
03-30 12:48:56.651: I/dalvikvm(15359): at android.graphics.YuvImage.nativeCompressToJpeg(Native Method)
03-30 12:48:56.667: I/dalvikvm(15359): at android.graphics.YuvImage.compressToJpeg(YuvImage.java:141)
03-30 12:48:56.667: I/dalvikvm(15359): at com.vfranchi.camerafluid.SavePhotoTask.doInBackground(SavePhotoTask.java:78)
03-30 12:48:56.667: I/dalvikvm(15359): at com.vfranchi.camerafluid.SavePhotoTask.doInBackground(SavePhotoTask.java:1)
03-30 12:48:56.667: I/dalvikvm(15359): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-30 12:48:56.667: I/dalvikvm(15359): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-30 12:48:56.667: I/dalvikvm(15359): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-30 12:48:56.667: I/dalvikvm(15359): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-30 12:48:56.667: I/dalvikvm(15359): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-30 12:48:56.667: I/dalvikvm(15359): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-30 12:48:56.667: I/dalvikvm(15359): at java.lang.Thread.run(Thread.java:856)
03-30 12:48:56.683: W/System.err(15359): java.lang.OutOfMemoryError
03-30 12:48:56.683: W/System.err(15359): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
03-30 12:48:56.683: W/System.err(15359): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
03-30 12:48:56.691: W/System.err(15359): at android.graphics.YuvImage.nativeCompressToJpeg(Native Method)
03-30 12:48:56.691: W/System.err(15359): at android.graphics.YuvImage.compressToJpeg(YuvImage.java:141)
03-30 12:48:56.691: W/System.err(15359): at com.vfranchi.camerafluid.SavePhotoTask.doInBackground(SavePhotoTask.java:78)
03-30 12:48:56.691: W/System.err(15359): at com.vfranchi.camerafluid.SavePhotoTask.doInBackground(SavePhotoTask.java:1)
03-30 12:48:56.691: W/System.err(15359): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-30 12:48:56.698: W/System.err(15359): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-30 12:48:56.698: W/System.err(15359): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-30 12:48:56.698: W/System.err(15359): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-30 12:48:56.698: W/System.err(15359): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-30 12:48:56.698: W/System.err(15359): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-30 12:48:56.706: W/System.err(15359): at java.lang.Thread.run(Thread.java:856)
03-30 12:48:56.706: D/skia(15359): ------- write threw an exception
03-30 12:49:15.181: W/jdwp(15359): Debugger is telling the VM to exit with code=1
03-30 12:49:15.181: I/dalvikvm(15359): GC lifetime allocation: 5 bytes
YuvImage 缓冲区是在 onPreviewFrame 上创建的,如下所示:
public void onPreviewFrame(byte[] data, Camera camera)
{
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
// Generate a YuvImage from the camera data
int w = size.width;
int h = size.height;
YuvImage photoImage = new YuvImage(data, parameters.getPreviewFormat(), w, h, null);
mPhotoBuffer.add(0, photoImage);
if (mPhotoBuffer.size() > mBufferSize)
{
mPhotoBuffer.remove(mPhotoBuffer.size() - 1);
}
}
最佳答案
我设法为我的问题找到了解决方案,而且是通过不使用 YuvImage 类。我猜 compressToJpeg() 方法有问题,至少在我的设备上我不知道。
这是我的 doInBackground 方法的最终代码:
protected Void doInBackground(Integer... params)
{
int lineHeight = mHeight / mBufferSize;
int currentHeight = 0;
Bitmap result = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
Log.d("output", "saving photo: "+mWidth+", "+mHeight);
File outputPath = new File(Environment.getExternalStorageDirectory().getPath() + "/camerafluid-output");
outputPath.mkdirs();
for (int i = 0; i < mBufferSize; i++)
{
File imageFile = new File(mCachePath, "image-"+i);
File outputFile = new File(outputPath, "image-"+i+".jpg");
if (!imageFile.exists())
{
Log.d("output", "image "+i+" not found on cache directory");
continue;
}
try
{
int size = (int)imageFile.length();
byte[] imageBytes = new byte[size];
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(imageFile));
buf.read(imageBytes, 0, size);
buf.close();
int[] imagePixels = MainActivity.convertYUV420_NV21toRGB8888(imageBytes, mWidth, mHeight);
imageBytes = null;
Bitmap bitmapImage = Bitmap.createBitmap(imagePixels, mWidth, mHeight, Bitmap.Config.ARGB_8888);
imagePixels = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (!bitmapImage.compress(CompressFormat.JPEG, 50, out))
{
Log.d("output", "problem converting yuv to jpg");
break;
}
FileOutputStream s = new FileOutputStream(outputFile);
s.write(out.toByteArray());
s.flush();
s.close();
s = null;
currentHeight += lineHeight;
Log.d("output", "currentHeight: "+currentHeight);
publishProgress((int)((i / (float)mBufferSize) * 100));
System.gc();
}
catch(Exception e)
{
}
if (isCancelled())
{
break;
}
}
return null;
}
方法是将 Yuv 数据转换为 RGB 并创建位图,以便我可以压缩为 JPEG 并保存到外部存储器。这仅用于测试目的,这样就不会出现 Signal error 11 或 OutOfMemory 异常。我已经尝试使用 40 张图像的缓冲区。
在此处找到 YUV -> RGB 转换器 Displaying YUV Image in Android .我正在使用 minhaz 想法在处理图像之前在磁盘上缓存缓冲区以避免高内存使用率。
关于android - 一次在多个 YuvImage 上使用 compresstojpeg 时内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696165/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t