1

I have created a CameraApp. And getting byte[] cameraPreviewCallback in onPreviewFrame(byte byteArray[] , Camera camera)

I am converting these byteArray to rgb and doing a lot of stuff too. So It is working slowly. So I think I should get benefit from java.util.concurrent package. But didn't find an easy way to handle what I want.

I want multiple threads to do my job serially but each time one can set bitmap to surfaceView. And also have to check that:

No Old byte of Array shouldn't be draw on surfaceView.

I think I have to write that code using a synchronize lock. And check for a boolean variable isWorkingOnFrame is true.

Can someone explain me how to use multiple threads in series but at one time one can do its work.

private android.hardware.Camera.PreviewCallback previewCallback = new android.hardware.Camera.PreviewCallback()
    {
        public void onPreviewFrame(byte abyte0[] , Camera camera)
        {   
            try
            {
               Thread.sleep(1L);
            }
            catch (InterruptedException interruptedexception)
            {
                return;
            }

            int[] rgbData =  YuvUtils.myDecodeGreyscale(abyte0, widthPreview, heightPreview, imageBrightness, nonGreencolorOffset, greenColorOffset);  ///it is working...

            editedBitmap.setPixels(rgbData, 0, widthPreview, 0, 0, widthPreview, heightPreview);

            if(CameraStatik.cameraId==CameraInfo.CAMERA_FACING_FRONT)
            {   
                matrix.setRotate(270F);
            }

            finalBitmap = Bitmap.createBitmap(editedBitmap, 0, 0, widthPreview, heightPreview, matrix, true);

            if(saveCurrentFrame)
            {
                saveCurrentFrame =false;

                new BitmapSaver(ctx, finalBitmap).start();
            }

            drawView.setBitmapToDraw(finalBitmap);
        }
    };

I simply want to make this code work efficiently.

Mauker
  • 11,237
  • 7
  • 58
  • 76
Xar-e-ahmer Khan
  • 1,314
  • 15
  • 23
  • 1 word for you - [ThreadPoolExecutor](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html) – Vinay W Aug 12 '15 at 13:55

1 Answers1

1

Solve it in the java way! Use the Executor interface along with Runnable.

First, get a ExecutorService (which implements the Executor interface) with Executors.newSingleThreadExecutor(), and then, put your Threads to work.

Example:

public YourClass extends Activity {
    // Other methods like onCreate() variable declarations and so on.
    // ...

    private void runOnThread() {
        Executor exe = Executors.newSingleThreadExecutor();
        exe.execute(new Runnable() { 
            public void run() { 
                // Do whatever you want to do.
            } 
        });
    }
    // Other methods...
    // ...
}

You can read more about Executor here.

Or like it was said on the comments, you can take a look at ThreadPoolExecutor and on this tutorial.

Mauker
  • 11,237
  • 7
  • 58
  • 76
  • I have two question. How many thread are created by your code and they work parallely or serially. – Xar-e-ahmer Khan Aug 12 '15 at 14:08
  • You can create as many Threads as you want. And they will work each one at a time. They'll be placed in a queue. Once one finishes working, the next one on the queue will begin to work. – Mauker Aug 12 '15 at 14:09
  • how could i handle to call setBitmap. Suppose ten threads created at a time. any suppose(third thread) draws bitmap and after 2 second thread 5 is calling setBitmap(it may have save the old set of bytes,how could i handle this. – Xar-e-ahmer Khan Aug 12 '15 at 14:13
  • If you create 10 Threads, they'll work only one at a time, so there'll be no concurrent calls to your `setBitmap()` method. – Mauker Aug 12 '15 at 14:15
  • You're not supposed to put exactly as it is there, it's just an example. I'll try to fit it on your code. – Mauker Aug 12 '15 at 15:01
  • 1
    Also note that there is no point of creating 10 threads if they are going to execute the tasks in series anyways... You'll be good with just one extra thread. – LordOfThePigs Aug 19 '15 at 10:33
  • @Mauker Nice answer. I have similar use case. I need to load 6 large bitmap images when Activity starts. So I would call runOnThread() in onCreate() to load the first image? Then set up a second method, runOnThreadSecond() and call it in onCreate() after runOnThread() and so on for the remainder? – AJW Sep 14 '20 at 04:44