正文

使用Camera類(14)

Android多媒體開發(fā)高級編程 作者:(美)艾佛瑞


 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 ? 讀書網(wǎng) m.ranfinancial.com 2005-2020, All Rights Reserved.
鄂ICP備15019699號 鄂公網(wǎng)安備 42010302001612號