I'm having a little trouble of getting an image/drawable or a bitmap from a SurfaceView that works as a camera preivew.
final CameraSurfaceView cameraSurfaceView = new CameraSurfaceView(this);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
ll.addView(cameraSurfaceView); // THIS WORKS
ImageView ivCam = (ImageView) findViewById(R.id.ivCam);
ivCam.setImageBitmap(cameraSurfaceView.getDrawingCache()); // THIS DOESN'T :(
Any suggestions? Thanks!
EDIT:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CameraSurfaceView cameraSurfaceView = new CameraSurfaceView(this);
LinearLayout ll = (LinearLayout)findViewById(R.id.LLS);
ll.addView(cameraSurfaceView); // THIS WORKS
}
///////////////////////////////////////////////////////////////////////////////////////////////
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder holder;
private Camera camera;
public CameraSurfaceView(Context context)
{
super(context);
//Initiate the Surface Holder properly
this.holder = this.getHolder();
this.holder.addCallback(this);
this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
this.camera = Camera.open();
this.camera.setPreviewDisplay(this.holder);
this.camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] _data, Camera _camera) {
Camera.Parameters params = _camera.getParameters();
int w = params.getPreviewSize().width;
int h = params.getPreviewSize().height;
int format = params.getPreviewFormat();
YuvImage image = new YuvImage(_data, format, w, h, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Rect area = new Rect(0, 0, w, h);
image.compressToJpeg(area, 50, out);
Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
ImageView ivCam = (ImageView) findViewById(R.id.imageView1);
ivCam.setImageBitmap(bm); /// NULL POINT EX HERE!
}
});
}
catch(IOException ioe)
{
ioe.printStackTrace(System.out);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
this.camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
this.camera.stopPreview();
this.camera.release();
this.camera = null;
}
public Camera getCamera()
{
return this.camera;
}
}