0

Am using Textureview.getBitmap() to grab frames every second from a Camera preview. it works perfectly except that it causes the main Ui thread to be Sluggish and sometimes even crash. please what do i need to do??.thanks

Here is my Code:

       mCountDownTimer=new CountDownTimer(7000,500) {

            @Override
            public void onTick(long millisUntilFinished) {
                Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
                i++;
                mProgressBar.setProgress(i);
               Bitmap bmps = mTextureView.getBitmap();
               ByteArrayOutputStream out = new ByteArrayOutputStream();
               bmps.compress(Bitmap.CompressFormat.JPEG , 0, out);
               rev.add(bmps);
            }

            @Override
            public void onFinish() {
            //Do what you want 
                i++;
                mProgressBar.setProgress(i);
                mCountDownTimer.cancel();

            }
        };
Donnie Ibiyemi
  • 971
  • 3
  • 15
  • 26

1 Answers1

1

The bmps.compress method is writing its results to your out ByteArrayOutputStream, and it doesn't change the original bitmap. So, first of all, your are not using the result of the compress method, as you are not using the 'out' variable after the bmps.compress call.

I think that removing these two lines:

ByteArrayOutputStream out = new ByteArrayOutputStream(); bmps.compress(Bitmap.CompressFormat.JPEG , 0, out);

Would produce the same results that you are having now.

But besides that, it is a good practice not to do processing (like image processing) on the UI thread, it will certainly slow down the app. The simplest way to solve this is using an AsyncTask:

http://developer.android.com/reference/android/os/AsyncTask.html

AsyncTask Android example

Try to compress the bitmap inside the doInBackground method of an AsyncTask and your problem will be solved.

Community
  • 1
  • 1