我对Android中的相机功能没有多少经验。我需要通过代码拍照,我打算将其发送回服务器。我将我的逻辑基于this帖子,只要我不将mCamera.takePicture(null, null, mPictureCallback);
添加到surfaceChange方法的末尾(这是我想要的),它就可以正常工作。当我添加该行时,它有时会起作用,但大多数时候,我只是回到黑屏。这是主要问题。我不认为该设备存在兼容性问题,因为我之前至少有3-4次使用此功能。
我正在测试的设备是Galaxy Nexus
public class CameraView extends Activity implements SurfaceHolder.Callback, OnClickListener
{
private static final String TAG = "CameraTest";
Camera mCamera;
boolean mPreviewRunning = false;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
Log.e(TAG, "onCreate");
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera_view);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
mSurfaceView.setOnClickListener(this);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
}
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
if (data != null)
{
//Intent mIntent = new Intent();
//mIntent.putExtra("image",imageData);
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
try
{
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length,opts);
bitmap = Bitmap.createScaledBitmap(bitmap, 480, 480, false);
CameraProjectActivity.image.setImageBitmap(bitmap);
}
catch(Exception e)
{
e.printStackTrace();
}
//StoreByteImage(mContext, imageData, 50,"ImageName");
//setResult(FOTO_MODE, mIntent);
setResult(585);
finish();
}
}
};
protected void onResume()
{
Log.e(TAG, "onResume");
super.onResume();
}
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}
protected void onStop()
{
Log.e(TAG, "onStop");
super.onStop();
}
public void surfaceCreated(SurfaceHolder holder)
{
Log.e(TAG, "surfaceCreated");
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(TAG, "surfaceChanged");
// XXX stopPreview() will crash if preview is not running
if (mPreviewRunning)
{
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();
Camera.Size previewSize = previewSizes.get(3);
p.setPreviewSize(previewSize.width, previewSize.height);
mCamera.setParameters(p);
try
{
mCamera.setPreviewDisplay(holder);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
// THIS IS THE CODE THAT BREAKS IT. IS THERE ANY OTHER WAY TO DO THIS??? ********
// mCamera.takePicture(null, null, mPictureCallback);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed");
// mCamera.stopPreview();
// mPreviewRunning = false;
// mCamera.release();
}
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
任何帮助将不胜感激。 谢谢!
答案 0 :(得分:2)
我不确定这是否是一个很好的解决方案,但我只是通过使surfaceChanged()方法同步并使线程在调用takePicture之前等待一秒来修复它。
try {
this.wait(1000);
mCamera.takePicture(null, null, mPictureCallback);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 1 :(得分:0)
我认为建议在startPreview()
之前致电setPreviewDisplay()
,但这不会产生重大影响。
您所看到的是相机初始化需要一些时间,并且在相机准备好之前无法拍照。最好使用来自摄像头的回调来确定它已经准备就绪,而不是等待任意1000毫秒。
您可以使用的回调是Camera.PreviewCallback。您可以在 CameraView 类中实现它。要触发回调,只需将setOneShotPreviewCallback(this)添加到 CameraView.surfaceChanged():
mCamera.startPreview();
mCamera.setPreviewDisplay(holder);
mCamera.setOneShotPreviewCallback(this);