I have a problem with camera. I want to get picture in onPreviewFrame but it's never called. I opened a camera, set preview Display and preview Callback but nothing. I just want to understand where I was wrong.
public class VideoCall extends Activity implements View.OnClickListener, Callback, PreviewCallback
{
TabHost thVideoChat;
Button btnVideoUp, btnVideoDown;
Handler uiHandler;
SurfaceView videoPrev;
SurfaceHolder surfaceHolder;
Camera camera;
Timer timer;
boolean getPic;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.a_video);
initialize();
Log.d("RAYZ", "onCreate");
}
private void initialize()
{
thVideoChat = (TabHost) findViewById(R.id.thVideoChat);
thVideoChat.setup();
TabSpec specs = thVideoChat.newTabSpec("1");
specs.setContent(R.id.tabVideo);
specs.setIndicator("Видео", getResources().getDrawable(R.drawable.mcam));
thVideoChat.addTab(specs);
specs = thVideoChat.newTabSpec("2");
specs.setContent(R.id.tabChat);
specs.setIndicator("Чат", getResources().getDrawable(R.drawable.mchat));
thVideoChat.addTab(specs);
btnVideoUp = (Button) findViewById(R.id.btnVideoUp);
btnVideoDown = (Button) findViewById(R.id.btnVideoDown);
btnVideoUp.setOnClickListener(this);
btnVideoDown.setOnClickListener(this);
videoPrev = (SurfaceView) findViewById(R.id.videoPrev);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
LayoutParams lp = videoPrev.getLayoutParams();
lp.height = 320;
lp.width = 240;
videoPrev.setLayoutParams(lp);
}
else
{
LayoutParams lp = videoPrev.getLayoutParams();
lp.height = 240;
lp.width = 320;
videoPrev.setLayoutParams(lp);
}
surfaceHolder = videoPrev.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
uiHandler = new Handler();
getPic = false;
}
@Override
protected void onPause()
{
Log.d("RAYZ", "onPause");
if (camera != null)
{
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
}
if (timer != null)
{
timer.cancel();
}
super.onPause();
}
@Override
protected void onResume()
{
super.onResume();
camera = Camera.open();
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnVideoUp:
{
btnVideoUp.setEnabled(false);
btnVideoDown.setEnabled(true);
timer = new Timer();
Log.d("RAYZ", "G_BTN");
timer.schedule(new TimerTask()
{
@Override
public void run()
{
uiHandler.post(new Runnable()
{
@Override
public void run()
{
getPic = true;
}
});
}
}, 0L, 1L * 500L);
break;
}
case R.id.btnVideoDown:
{
btnVideoUp.setEnabled(true);
btnVideoDown.setEnabled(false);
Log.d("RAYZ", "R_BTN");
timer.cancel();
timer = null;
break;
}
default:
break;
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera)
{
Log.d("RAYZ", "getPic");
// if (getPic)
// {
// }
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(this);
camera.startPreview();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
}
Tried this code on 2 other devices (phones HTS and Sony Xperia) and everything worked fine. But on my tablet it does not work. I'm confused.