拍照

拍照
public class MainActivity extends Activity {
    private View layout;
    private Camera camera;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Activity没有标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
        setContentView(R.layout.main);
        
        layout = this.findViewById(R.id.buttonlayout);
        
        SurfaceView surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
        //设置直接在SurfaceView上显示数据,不用对数据进行缓存
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //设置SurfaceView控的大小
        surfaceView.getHolder().setFixedSize(176, 144);
        //设置屏幕高亮显示
        surfaceView.getHolder().setKeepScreenOn(true);
        //对SurfaceView控设置回调方法
        surfaceView.getHolder().addCallback(new SurfaceCallback());
    }
    
    public void takepicture(View v){
        if(camera!=null){
            switch (v.getId()) {
            case R.id.takepicture:
                camera.takePicture(null, null, new MyPictureCallback());//拍照,MyPictureCallback()处理照片
                break;

            case R.id.autofocus:
                camera.autoFocus(null);
                break;
            }
        }
    }
    
    private final class MyPictureCallback implements PictureCallback{
        public void onPictureTaken(byte[] data, Camera camera) {
            try {
                //保存照片的数据
                File jpgFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg");
                FileOutputStream outStream = new FileOutputStream(jpgFile);
                outStream.write(data);
                outStream.close();
                //设置摄像头重新预览
                camera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
    
    private final class SurfaceCallback implements Callback{
        public void surfaceCreated(SurfaceHolder holder) {
            try{
                camera = Camera.open();//打开摄像头
                Camera.Parameters parameters = camera.getParameters();
                //Log.i("MainActivity", parameters.flatten());
                //设置摄像头的各项参数
                parameters.setPreviewSize(800, 480);
                parameters.setPreviewFrameRate(5);
                parameters.setPictureSize(1024,768);
                parameters.setJpegQuality(80);
                camera.setParameters(parameters);
                //设置预览图片显示到那个控件上
                camera.setPreviewDisplay(holder);
                camera.startPreview();//开始预览
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            if(camera!=null){
                //当SurfaceView对象destroy时,释放掉摄像头
                camera.release();
                camera = null;
            }
        }
        
    }

    @Override
        //处理屏幕触摸事件
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            layout.setVisibility(ViewGroup.VISIBLE);
            return true;
        }
        return super.onTouchEvent(event);
    }
    
    
}
拍照

拍照

上一篇:与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast)


下一篇:使用SelectClipRgn注意事项