问题描述
我的相机有问题.我想在onPreviewFrame中获取图片,但从未调用过.我打开相机,设置预览显示和预览回叫,但是什么也没有.我只想了解我错了.
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)
{
}
}
在其他2台设备(手机HTS和Sony Xperia)上尝试了此代码,一切正常.但是在我的平板电脑上它不起作用.我很困惑.
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.
推荐答案
一段时间以来,我一直在努力解决这个问题,而我的解决方案是在SurfaceView.surfaceChanged中的Camera.setPreviewDisplay之后立即调用Camera.setPreviewCallback:
I have been struggling with this problem for sometime and the solution for me was to call Camera.setPreviewCallback just after Camera.setPreviewDisplay in SurfaceView.surfaceChanged:
public void onCreate(Bundle state) { log("onCreate");
try {
super.onCreate(state);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
surface = (SurfaceView) findViewById(R.id.surface);
int type = SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS;
surface.getHolder().setType(type);//REQUIRED:API10
surface.getHolder().addCallback(this);
camera = Camera.open();
camera.setDisplayOrientation(90); // portrait mode only
} catch (Exception e) {
showException(e);
}
}
public void surfaceChanged(SurfaceHolder sh, int format, int w, int h) { log("surfaceChanged");
try {
camera.stopPreview();
Size s = camera.getParameters().getPreviewSize();
LayoutParams params = surface.getLayoutParams();
params.height = w*s.width/s.height; // portrait mode only
surface.setLayoutParams(params);
camera.setPreviewDisplay(sh);
camera.setPreviewCallback(this);
camera.startPreview();
} catch (Exception ex) {
showException(ex);
}
}
阅读大量文章后,我意识到如果相机没有完全初始化的SurfaceHolder,则setPreviewCallback可能会失败.
Reading numerous article I realized the setPreviewCallback may fail if the camera does not have a fully initialized SurfaceHolder.
我希望这对您有帮助...
I hope this helps ...
这篇关于不调用Android onPreviewFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!