我已经尝试了所有可以找到的解决方案,但没有一个能够提供帮助。他们中的大多数都在谈论回收位图,这对我来说不是一个选择。我的应用程序在与UI的单独线程中进行一些图像处理,然后我使用绘制在我的图像处理算法返回的位置绘制实心圆。这是我的代码 -
private Runnable runNetwork = new Runnable()
{
public void run()
{
if(!destroyFlag)
{
bProcessing = true;
start = SystemClock.elapsedRealtime();
locations = callTorch(torchState, PreviewSizeWidth, PreviewSizeHeight, FrameData,networkLoop);
stop = SystemClock.elapsedRealtime();
wholeLoop = (float) ((stop-start)*0.001);
fps = 1/wholeLoop;
start = SystemClock.elapsedRealtime();
if(locations[0] != 0)
{
bitmap.eraseColor(0);
for(int i = 0; i < locations.length; i+=4)
{
if(locations[i+2] != 0)
{
mCanvas.drawCircle(locations[i], locations[i+1], 10, p);
mCanvas.drawText("Face", locations[i], locations[i+1]+15, p);
}
}
}
else
bitmap.eraseColor(0);
MycameraClass.setImageBitmap(bitmap);
stop = SystemClock.elapsedRealtime();
displayLoop = (float) ((stop-start) * 0.001);
networkPercentage = (networkLoop/wholeLoop)*100;
CameraActivity.detailsText.setText("****Profiling information****\n" + "Time for network = " + networkLoop + " s\nTime for whole loop = " +
wholeLoop + " s\nNetwork takes " + networkPercentage + "% of computation time" + "\nTime for display = " + displayLoop + "s \nFrame rate =" + fps);
bProcessing = false;
}
}
};
我重复使用相同的位图来绘制,所以我无法回收它。这有什么原因导致内存泄漏吗?这也是一个非常糟糕的。我的应用程序在崩溃之前几乎不能运行半分钟。
编辑:更多代码片段与位图创建相关
public class CameraClass implements SurfaceHolder.Callback, Camera.PreviewCallback
{
private Camera mCamera = null;
private ImageView MycameraClass = null;
public Bitmap bitmap = null;
private int[] pixels = null;
private byte[] FrameData = null;
private int imageFormat;
private int PreviewSizeWidth;
private int PreviewSizeHeight;
private boolean bProcessing = false;
float start, stop, networkLoop, wholeLoop, fps, displayLoop, networkPercentage = 0;
Handler mHandler = new Handler(Looper.getMainLooper());
public long torchState = 0;
private boolean destroyFlag = false;
Paint p = new Paint();
Canvas mCanvas;
int[] locations;
public CameraClass(int PreviewlayoutWidth, int PreviewlayoutHeight, ImageView cameraClass)
{
PreviewSizeWidth = PreviewlayoutWidth;
PreviewSizeHeight = PreviewlayoutHeight;
MycameraClass = cameraClass;
bitmap = Bitmap.createBitmap(1280, 768, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(bitmap);
p.setAntiAlias(true);
p.setColor(Color.RED);
p.setStrokeWidth((float)15.0);
p.setTextSize(100);
}
上面是创建位图的类。这个类是从下面显示的Activity中调用的 -
public class CameraActivity extends Activity
{
private FrameLayout mainLayout;
private ImageView MyCameraClass = null;
private CameraClass camPreview;
private int PreviewSizeWidth, PreviewSizeHeight;
static AssetManager assetManager;
static TextView detailsText;
Button back;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
assetManager = getAssets();
setContentView(R.layout.activity_camera);
back = (Button) findViewById(R.id.back);
back.setOnClickListener(buttonHandler);
detailsText = (TextView) this.findViewById(R.id.details);
MyCameraClass = new ImageView(this);
SurfaceView camView = new SurfaceView(this);
SurfaceHolder camHolder = camView.getHolder();
PreviewSizeWidth = MainActivity.width;
PreviewSizeHeight = MainActivity.height;
camPreview = new CameraClass(PreviewSizeWidth, PreviewSizeHeight, MyCameraClass);
camHolder.addCallback(camPreview);
camHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mainLayout = (FrameLayout) findViewById(R.id.frameLayout);
mainLayout.addView(camView, new LayoutParams(1280, 768));
mainLayout.addView(MyCameraClass, new LayoutParams(1280, 768));
}