見つけたすべての解決策を試しましたが、どれも役に立ちません。彼らのほとんどは、私にとって選択肢ではないビットマップのリサイクルについて話しています。私のアプリは、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;
}
}
};
同じビットマップを再利用して描画するため、リサイクルできません。これがメモリリークを引き起こす理由はありますか? それもかなり悪いものです。私のアプリは、クラッシュする前に 30 分しか実行できません。
編集: ビットマップの作成に関連するより多くのコード フラグメント
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);
}
上記は、ビットマップを作成するクラスです。このクラスは、以下に示すアクティビティから呼び出されます -
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));
}