Android 自制照相机

需要权限:   
//写人sdk的权限

//调用摄像头的权限 



package com.geek.photograph;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;


public class MainActivity extends Activity {
    /** Called when the activity is first created. */
SurfaceView surfaceView;
SurfaceHolder sholder;
Camera camera;

File file;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        surfaceView=(SurfaceView) findViewById(R.id.sv_viewid);
        
        sholder=surfaceView.getHolder();
        //设置屏幕分辨率
        sholder.setFixedSize(300, 300);
        //表示不使用SurfaceView自身的缓冲机制,把得到的结果直接输出
        sholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //保持屏幕常亮
        sholder.setKeepScreenOn(true);
        //surfaceview这个控件,只有activity显示到了前台,他才会被创建
        sholder.addCallback(new MyCallback());      
    }
    
 class MyCallback implements Callback{

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
//打开摄像头
camera=Camera.open();
//设置参数
Parameters parameters= camera.getParameters();
//设置品质(0-100)
parameters.setJpegQuality(80);
//设置照片格式
parameters.setPictureFormat(ImageFormat.JPEG);
//设置照片大小
parameters.setPreviewSize(300, 300);
//设置预览显示方向
camera.setDisplayOrientation(90);
try {
//设置预览显示
camera.setPreviewDisplay(sholder);
//开始预览
camera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}
   
    }
    
    public void click(View view){
    int id=view.getId();
    switch (id) {
case R.id.bt_photographid:
//shutter开门被按下   raw  相机所捕获的原始数据      jpeg  相机处理后的数据
camera.takePicture(null, null,new Mypicturecallback());
break;
        case R.id.bt_focusid:
        //自动聚焦
        camera.autoFocus(null);
        break;
default:
break;
}
    }
    
    class Mypicturecallback implements  PictureCallback{


@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
try {
//拿到一个文件实例
file=new File(Environment.getExternalStorageDirectory()+"/hehe.jpg");
            //输出流
FileOutputStream fos=new  FileOutputStream(file);
//把文件保存到sdk里面
fos.write(data);
} catch (Exception e) {
e.printStackTrace();
}
 
//在拍照的时候相机是被占用的,需要去处理照片,所以拍完照片之后需要重写预览
              camera.startPreview();
              
            //跳转显示刚刚拍的照片
// Intent intent = new Intent(getApplicationContext(),ShowPictureActivity.class);
// intent.putExtra("picture", picture.getName());
// startActivity(intent);


}
   
    }
}

你可能感兴趣的:(Android)