定制一个Camera
1.权限:
3.初始化:
sf_suface = (SurfaceView) findViewById(R.id.sf_camera);
sf_holder = sf_suface.getHolder();
sf_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//推送类型
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
5.告诉SurfaceHolder实现Callback
sf_holder.addCallback(this);
6.实现Camera对象
@Override
public void surfaceCreated(SurfaceHolder arg0) {
camera = Camera.open();
}
7.显示并启动Camera
@Override
public void surfaceCreated(SurfaceHolder arg0) {
camera = Camera.open();
try {
camera.setPreviewDisplay(sf_holder);
} catch (IOException e) {
camera.release();
e.printStackTrace();
}
camera.startPreview();
}
8.记得释放camera
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
camera.stopPreview();
camera.release();
}
运行截图:
发现有些奇怪逆时针旋转了90度 ,切换横屏发现在横屏状态下是正确的。
1需要旋转:
@Override
public void surfaceCreated(SurfaceHolder arg0) {
camera = Camera.open();
try {
Camera.Parameters parameters = camera.getParameters();
//竖屏
if(this.getResources().getConfiguration().orientation!=Configuration.ORIENTATION_LANDSCAPE){
parameters.set("orientation", "portrait");
camera.setDisplayOrientation(90);
parameters.setRotation(90);
}else{
parameters.set("orientation", "landscape");
camera.setDisplayOrientation(0);
parameters.setRotation(0);
}
camera.setParameters(parameters);
camera.setPreviewDisplay(sf_holder);
} catch (IOException e) {
camera.release();
e.printStackTrace();
}
camera.startPreview();
}
Parameters 还有很多有趣的参数 可以自己测试一下。
这里的模式一定需要Camera支持
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);//聚焦模式
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);//闪光灯模式
parameters.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);//情景模式
parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);//白平衡模式
parameters.setAntibanding(Camera.Parameters.ANTIBANDING_AUTO);//抗条带
//各种效果
List colorEffects = parameters.getSupportedColorEffects();
Iterator cEffects = colorEffects.iterator();
while(cEffects.hasNext()){
String colorEffect = cEffects.next();
if(colorEffect.equals(Camera.Parameters.EFFECT_NEGATIVE)){
parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);//照片特效模式
break;
}
}
更改摄像头预览大小:
这里不指定一个确定的值,选择不超过一对常量的大小:
private static final int width = 300;
private static final int heigth = 300;
@Override
public void surfaceCreated(SurfaceHolder arg0) {
camera = Camera.open();
int mwidth = 0;
int mheight = 0;
try {
Camera.Parameters parameters = camera.getParameters();
// 竖屏
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "portrait");
camera.setDisplayOrientation(90);
parameters.setRotation(90);
} else {
parameters.set("orientation", "landscape");
camera.setDisplayOrientation(0);
parameters.setRotation(0);
}
//屏幕大小
List sizes = parameters.getSupportedPreviewSizes();
if(sizes.size()>1){
Iterator iterator = sizes.iterator();
while (iterator.hasNext()) {
Camera.Size size = iterator.next();
if(size.width>mwidth&&size.width<=width&&size.height>mheight&&size.height<=heigth){
mwidth = size.width;
mheight =size.height;
}
}
}
if(mwidth!=0&&mheight!=0){
parameters.setPictureSize(mwidth, mheight);
sf_camera.setLayoutParams(new LinearLayout.LayoutParams(mwidth, mheight));
}
camera.setParameters(parameters);
camera.setPreviewDisplay(sf_holder);
} catch (IOException e) {
camera.release();
e.printStackTrace();
}
camera.startPreview();
}
保存图片实现PictureCallback:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//向MeidaStore插入一条记录
Uri imageUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
try {
OutputStream ops = getContentResolver().openOutputStream(imageUri);
ops.write(data);
ops.flush();
ops.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//更新记录标题和描述
private void updatecontentValues(Uri imageUri){
ContentValues contentValues = new ContentValues(3);
contentValues.put(Media.DISPLAY_NAME, "this is test update title");
contentValues.put(Media.DESCRIPTION, "this is test update des");
getContentResolver().update(imageUri, contentValues, null, null);
}
初始化 实现OnClickListener
sf_camera.setFocusable(true);
sf_camera.setFocusableInTouchMode(true);
sf_camera.setClickable(true);
sf_camera.setOnClickListener(this);
@Override
public void onClick(View v) {
camera.takePicture(null, null, null,this);
}