How to display captured preview frame on dispaly

All your problems with Audio, Video and Images.

How to display captured preview frame on dispaly

Postby sri_reddy523 » Sat Apr 18, 2009 1:47 am

Hi

I am working on previewcallback approach to capture frames.I am trying to display captured frame on canvas, but blank screen appearing on display.Please suggest me if I am doing any mistake.

public class ImageCapture extends Activity implements SurfaceHolder.Callback
{
private Camera camera;
private boolean isPreviewRunning = false;
public int [] outdata1 = new int[320*480];

private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;


public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
Log.e(getClass().getSimpleName(), "onCreate");
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
surfaceView = (SurfaceView)findViewById(R.id.surface);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}
public void onDraw(Canvas canvas){


canvas.drawBitmap(outdata1, 0, 320, 320, 480, 320, 480, false, null);
//surfaceView.invalidate();
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
}
Camera.PreviewCallback mpreviewcallback = new Camera.PreviewCallback (){
public void onPreviewFrame(byte[] data, Camera camera)
{
camera.startPreview();
{ Log.e(getClass().getSimpleName(), "SP onPictureTaken = " + data[1] + " length = " + data.length+" "+Long.toString(System.currentTimeMillis()));
} }
};

protected void onResume()
{
Log.e(getClass().getSimpleName(), "onResume");
super.onResume();
}

protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}

protected void onStop()
{
Log.e(getClass().getSimpleName(), "onStop");
super.onStop();
}

public void surfaceCreated(SurfaceHolder holder)
{
Log.e(getClass().getSimpleName(), "surfaceCreated");
camera = Camera.open();
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
Log.e(getClass().getSimpleName(), "surfaceChanged");
if (isPreviewRunning) {
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
Camera.Size sss = p.getPreviewSize();
Log.e(getClass().getSimpleName(), "befoe set### job started calling picture capture SP "+sss.height+" "+sss.width+" "+p.getPreviewFrameRate());
p.setPreviewSize(640, 480);
p.setPreviewFrameRate(1);

camera.setParameters(p);

ImageCaptureCallback iccb = new ImageCaptureCallback ();
camera.setPreviewCallback(iccb);
Camera.Parameters pp = camera.getParameters();
Camera.Size ss = pp.getPreviewSize();
Log.e(getClass().getSimpleName(), "$$$$$$ job started calling picture capture SP "+ss.height+" "+ss.width+" "+pp.getPreviewFrameRate());
camera.setPreviewDisplay(holder);
camera.startPreview();
isPreviewRunning = true;

}

public void surfaceDestroyed(SurfaceHolder holder)
{
Log.e(getClass().getSimpleName(), "surfaceDestroyed");
camera.stopPreview();
isPreviewRunning = false;
camera.release();
}
public void imagedata(int[] data){

this.outdata1 = data;
Log.e(getClass().getSimpleName(), "#@$@$$%%^ data received");
surfaceView.invalidate();
}
}


--------------------------

public class ImageCaptureCallback implements PreviewCallback {


public static int x=1;
//public static int[] out;
public void onPreviewFrame(byte[] data, Camera camera) {


try {
//Log.e(getClass().getSimpleName(), "#@$@$$%%^ data received");
if (x==1){
x=0;
byte[] data1 = new byte [320*480*(3/2)];
for (int m = 0; m <(320*480*(3/2));m++ )
data1[m]=data[m];
datadecoding req = new datadecoding( data1);
Thread t = new Thread(req);
t.start();
}

}

catch(Exception ex) {
ex.printStackTrace();
}
}
public static void imagedata(int[] outdata) {
// TODO Auto-generated method stub

}
}



class datadecoding implements Runnable {

byte[] data3;
public static int [] outdata = new int[320*480];
int w =320;
int h = 480;


public datadecoding(byte[] data1) throws Exception
{
this.data3 =data1;
}

public void run () {

try {
outdata = decodeYUV( data3, w, h);

ImageCaptureCallback.imagedata(outdata);
//ImageCaptureCallback.invalidate();
ImageCaptureCallback.x = 1;
}
catch(Exception e)
{
System.out.println(e);
}
}

public int[] decodeYUV( byte[] fg, int width, int height) throws NullPointerException, IllegalArgumentException {
final int sz = width * height;
int [] out = new int[320*480];
if(out == null) throw new NullPointerException("buffer 'out' is null");
if(out.length < sz) throw new IllegalArgumentException("buffer 'out' size " + out.length + " < minimum " + sz);
if(fg == null) throw new NullPointerException("buffer 'fg' is null");
if(fg.length < sz) throw new IllegalArgumentException("buffer 'fg' size " + fg.length + " < minimum " + sz * 3/ 2);
int i, j; int Y, Cr = 0, Cb = 0;
for(j = 0; j < height; j++)
{
int pixPtr = j * width;
final int jDiv2 = j >> 1;
for(i = 0; i < width; i++){
Y = fg[pixPtr];
if(Y < 0) Y += 255;
if((i & 0x1) != 1)
{
final int cOff = sz + jDiv2 * width + (i >> 1) * 2;
Cb = fg[cOff];
if(Cb < 0) Cb += 127;
else Cb -= 128;
Cr = fg[cOff + 1];
if(Cr < 0) Cr += 127;
else Cr -= 128; }
int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
if(R < 0)
R = 0;
else if(R > 255)
R = 255;
int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
if(G < 0) G = 0;
else if(G > 255) G = 255;
int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
if(B < 0)
B = 0;
else if(B > 255) B = 255;
out[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
}
}


return out;
}


}
sri_reddy523
Freshman
Freshman
 
Posts: 3
Joined: Fri Mar 27, 2009 8:18 am

Top

Postby injection18 » Tue Apr 21, 2009 8:40 am

you have to comment this line:

surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
injection18
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Apr 21, 2009 8:38 am

Postby androiduser123 » Sat May 16, 2009 12:31 pm

Hi sri_reddy523,

I am also trying to display preview frames on canvas.

Could you solve this problem?
androiduser123
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 12, 2009 7:20 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 12 guests