Android相机教程(使用surfaceview)

这是我的示例代码:

package newslab.video.server; import android.app.Activity; import android.hardware.Camera; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.TextView; public class VideoServer extends Activity implements SurfaceHolder.Callback { TextView testView; Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; private final String tag = "VideoServer"; Button start, stop; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start = (Button)findViewById(R.id.btn_start); start.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { start_camera(); } }); stop = (Button)findViewById(R.id.btn_stop); stop.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { stop_camera(); } }); surfaceView = (SurfaceView)findViewById(R.id.surfaceView1); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } private void start_camera() { try{ camera = Camera.open(); }catch(RuntimeException e){ Log.e(tag, "init_camera: " + e); return; } Camera.Parameters param; param = camera.getParameters(); //modify parameter param.setPreviewFrameRate(20); param.setPreviewSize(176, 144); camera.setParameters(param); try { camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); } catch (Exception e) { Log.e(tag, "init_camera: " + e); return; } } private void stop_camera() { camera.stopPreview(); camera.release(); } public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub } public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } } 

但是,它只显示预览并停止相机。
是否有任何其他的教程进行进一步的处理,如如何保存图像或用位图处理图像?

好。 所以这里是解决scheme:

我不添加任何try catch。 这只是为了捕获图像并将其存储到SD卡中。

 public class VideoServer extends Activity implements SurfaceHolder.Callback { TextView testView; Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; PictureCallback rawCallback; ShutterCallback shutterCallback; PictureCallback jpegCallback; private final String tag = "VideoServer"; Button start, stop, capture; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start = (Button)findViewById(R.id.btn_start); start.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { start_camera(); } }); stop = (Button)findViewById(R.id.btn_stop); capture = (Button) findViewById(R.id.capture); stop.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { stop_camera(); } }); capture.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub captureImage(); } }); surfaceView = (SurfaceView)findViewById(R.id.surfaceView1); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); rawCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { Log.d("Log", "onPictureTaken - raw"); } }; /** Handles data for jpeg picture */ shutterCallback = new ShutterCallback() { public void onShutter() { Log.i("Log", "onShutter'd"); } }; jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { FileOutputStream outStream = null; try { outStream = new FileOutputStream(String.format( "/sdcard/%d.jpg", System.currentTimeMillis())); outStream.write(data); outStream.close(); Log.d("Log", "onPictureTaken - wrote bytes: " + data.length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } Log.d("Log", "onPictureTaken - jpeg"); } }; } private void captureImage() { // TODO Auto-generated method stub camera.takePicture(shutterCallback, rawCallback, jpegCallback); } private void start_camera() { try{ camera = Camera.open(); }catch(RuntimeException e){ Log.e(tag, "init_camera: " + e); return; } Camera.Parameters param; param = camera.getParameters(); //modify parameter param.setPreviewFrameRate(20); param.setPreviewSize(176, 144); camera.setParameters(param); try { camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); //camera.takePicture(shutter, raw, jpeg) } catch (Exception e) { Log.e(tag, "init_camera: " + e); return; } } private void stop_camera() { camera.stopPreview(); camera.release(); } public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub } public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } } 

开始之前不要按caputurebutton,因为我没有检查它。 我只是想告诉你,如何捕捉和存储图像到SD卡

不要忘了在Android Manifest文件中使用相机的权限,

 <uses-permission android:name="android.permission.CAMERA" ></uses-permission> 

你也必须获得在SD卡上存储图像的权限,

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

manifiest文件中的相机权限:

 <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA" /> 

manifiest文件中存储来自sdcard图像的权限:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

我有相机问题,搞清楚如何实施和处理,并遇到这个资源: 相机预览 。 在页面的底部,您可以看到最常见的场景以及原始问题中的例子: 拍摄并预览照片