布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <SurfaceView android:id="@+id/surface" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="6" > <Button android:id="@+id/button" android:text="拍照" android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="takepictrue" android:layout_weight="1" /> <Button android:id="@+id/autofocuse" android:text="对焦" android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="takepictrue" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
入口文件:
public class MainActivity extends Activity {
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
SurfaceView surfaceView=(SurfaceView) findViewById(R.id.surface);
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//自己不维护缓冲区,直接输出到屏幕上
surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
surfaceView.getHolder().setKeepScreenOn(true);//设置高亮
surfaceView.getHolder().addCallback(new SurfaceCallback());//回调函数,通知什么时候调用
}
public void takepictrue(View v){
if (camera!=null) {
switch (v.getId()) {
case R.id.button:
camera.takePicture(null, null, new MyPictureCallback());
break;
case R.id.autofocuse:
camera.autoFocus(null);
break;
}
}
}
private final class MyPictureCallback implements PictureCallback{
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
try {
FileOutputStream os=new FileOutputStream(file);
os.write(data);
os.close();
camera.startPreview();//开始预览
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private final class SurfaceCallback implements Callback{
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
try {
camera=Camera.open();//打开摄像头
Camera.Parameters parameters=camera.getParameters();//得到摄像头的参数
//System.out.println("摄像头参数:"+parameters.flatten());//得到摄像头可设置的参数
parameters.setPreviewSize(480, 320);
parameters.setPreviewFrameRate(5);
parameters.setPictureSize(480, 320);
parameters.setJpegQuality(80);
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
camera.startPreview();//开始预览
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera!=null) {
camera.release();//关闭摄像头
}
}
}
}
权限:
<!-- 照相机权限 --> <uses-permission android:name="android.permission.CAMERA"/> <!-- 在SD卡中创建和删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 在SD卡中写入权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
转载于:https://blog.51cto.com/shaotao/1298283