4

I'm a complete noob . I've managed to write and understand this code after reading this http://developer.android.com/guide/topics/media/camera.html . But now i want to get the byte array for preview and then convert it to bitmap . But i want to do this in real time without be forced to save a picture file in storage . Please , help! Here is my program code.

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        mCamera.setPreviewDisplay(holder);

        mCamera.startPreview();
    } catch (IOException e) {
        String TAG = null;
        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.


    mCamera.setDisplayOrientation(90);
    // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {

        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e){
        String TAG = null;
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
}
private PictureCallback mPicture = new PictureCallback(){

    @Override
    public void onPictureTaken(byte[] data, Camera camera)

    {
        // TODO: Implement this method

    }
};

}

And main activity :

public class MainActivity extends Activity
{   private Camera mCamera;
private CameraPreview mPreview; 
int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
    mCamera = getCameraInstance();
    mPreview = new CameraPreview(this, mCamera);

    FrameLayout preview = (FrameLayout) findViewById(R.id.frame);
    preview.addView(mPreview);



} 
public static Camera getCameraInstance()
{
    Camera c = null;
    try
    {
        c = Camera.open();}
    catch (Exception e)
    { System.out.println("blamjjjh");}
    return c;
}
public void releasec(){
    mCamera.release();
}

@Override
protected void onStop()
{

    super.onStop();
    releasec();
}
}
Madalin
  • 385
  • 1
  • 5
  • 18

1 Answers1

2

As detailed in the Android Developer docs here (which you might have already read), add an implementation of the PictureCallback interface (see the example below) to your Activity. Also you can use BitmapFactory to then convert the byte array that gets passed back to a Bitmap. Then you can use this as required.

NOTE: I would also read the docs here on handling Bitmaps efficiently in relation to memory as you might get OutOfMemory errors if you're manipulating Bitmaps.

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

       //create a Bitmap from the byte array
       Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);

       //use your Bitmap
    }
};

You then need to pass this into the takePicture() method against your camera instance e.g.

// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // get an image from the camera
            mCamera.takePicture(null, null, mPicture);
        }
    }
);

Hope this helps! :-)

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Andy B
  • 753
  • 5
  • 12
  • Thanks for the answer , but I think you don't understood what I want to do. I want to do some real-time processing directely on preview , without using a button for capturing a picture and then process it . I want to do the processing while the preview is displayed . To be more precise I want to take a picture everytime the surface has been changed in preview . – Madalin Apr 02 '15 at 22:05
  • Arh, ok my fault. That is certainly more complicated so will leave for another to answer unless I can think of an answer in the mean time. – Andy B Apr 03 '15 at 06:46
  • Actually I've the same requirements as yours and I'm stuck right where you were 4 years ago. If you have the solution, can you please post that? – Mohd Zaid May 08 '19 at 14:06
  • 1
    Yes. I have solved it, I will post a solution in a short time. (I am unable to post it right now) – Madalin May 09 '19 at 20:59
  • 1
    @Madalin did you post it in the meantime? – Lorenz Jul 29 '19 at 12:17
  • @Madalin, I'm having the same problem how did you solve it? – KaramJaber Oct 18 '21 at 10:06
  • https://stackoverflow.com/questions/32063524/how-to-get-each-frame-data-using-camera2-api-in-android5-0-in-realtime please check out this question for more clarifications – Madalin Oct 18 '21 at 12:13