android之使用摄像头拍照
转载在main.xml布局文件添加用于显示取景画面的SurfaceView 控件:
<SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />
SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);
surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera camera = Camera.open();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小
parameters.setPreviewFrameRate(3);//每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式
parameters.set("jpeg-quality", 85);//照片质量
parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面
camera.startPreview();//开始预览
camera.autoFocus(null);//自动对焦
camera.takePicture(null, null, null, jpegCallback);//拍照片
camera.stopPreview();//停止预览
camera.release();//释放摄像头
添加的权限:<uses-permission android:name="android.permission.CAMERA"/>
代码:
public class TakePictureActivity extends Activity {
private static final String TAG = "TakePictureActivity";
private SurfaceView surfaceView;
private Camera camera;
private boolean preview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//高亮
setContentView(R.layout.main);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(new SufaceListener());
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
}
private final class SufaceListener implements SurfaceHolder.Callback{
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();//打开摄像头
Camera.Parameters parameters = camera.getParameters();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小
parameters.setPreviewFrameRate(3);//每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式
parameters.set("jpeg-quality", 85);//照片质量
parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面
camera.startPreview();
preview = true;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(camera!=null){
if(preview) camera.stopPreview();
camera.release();
camera = null;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(camera!=null && event.getRepeatCount()==0){
switch (keyCode) {
case KeyEvent.KEYCODE_SEARCH:
camera.autoFocus(null);//自动对焦
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_CAMERA:
//拍照
camera.takePicture(null, null, new PictureCallbackListener());
break;
}
}
return true;
}
private final class PictureCallbackListener implements Camera.PictureCallback{
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File file = new File(Environment.getExternalStorageDirectory(), "itcast.jpg");
FileOutputStream outStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, outStream);
outStream.close();
//重新浏览
camera.stopPreview();
camera.startPreview();
preview = true;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
}
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
网络监控摄像头通过OBS取流变身电脑摄像头
OBS 网络摄像头接入腾讯会议
Windows 视频流 用户名 -
Android调用摄像头拍照
添加权限: <!-- 摄像头权限 --> <uses-permission android:name="
移动开发 android ide