正文

使用Camera类(14)

Android多媒体开发高级编程 作者:(美)艾佛瑞


 setContentView(R.layout.main);

          cameraView = (SurfaceView) this.findViewById(R.id.CameraView);

          surfaceHolder = cameraView.getHolder();

          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

          surfaceHolder.addCallback(this);

          cameraView.setFocusable(true);

          cameraView.setFocusableInTouchMode(true);

          cameraView.setClickable(true);

          cameraView.setOnClickListener(this);

     } 

     public void onClick(View v) {

          camera.takePicture(null, null, this);

     }

随后是之前所描述的onPictureTaken方法。

public void onPictureTaken(byte[] data, Camera camera) {

     Uri imageFileUri = 

     getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, 

new ContentValues());

    try {

          OutputStream imageFileOS = 

             getContentResolver().openOutputStream(imageFileUri);

          imageFileOS.write(data);

          imageFileOS.flush();

          imageFileOS.close();

    } catch (FileNotFoundException e) {

          Toast t = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);

          t.show();

    } catch (IOException e) {

          Toast t = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);

          t.show();

    }

    camera.startPreview();

}


上一章目录下一章

Copyright © 读书网 www.dushu.com 2005-2020, All Rights Reserved.
鄂ICP备15019699号 鄂公网安备 42010302001612号