如何在cick事件中访问camera.java?

时间:2010-05-25 10:44:18

标签: android

我正在制作一个应用程序,点击按钮上的照片

我有camera.java操作相机并拍照

如何在下面的活动中调用它?

public void onClick(DialogInterface arg0,int arg1){

的setContentView(R.layout.startcamera);

            }

相机.java

package neuro.com;

import java.io.FileNotFoundException;  import java.io.FileOutputStream;  import java.io.IOException;

import android.app.Activity;  import android.hardware.Camera;  import android.hardware.Camera.PictureCallback;  import android.hardware.Camera.ShutterCallback;  import android.os.Bundle;  import android.util.Log;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.FrameLayout;

公共类CameraDemo扩展Activity {     private static final String TAG =“CameraDemo”;     相机相机;     预览预览;     按钮按钮;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startcamera);

    preview = new Preview(this);
    ((FrameLayout) findViewById(R.id.preview)).addView(preview);

    buttonClick = (Button) findViewById(R.id.buttonClick);
    buttonClick.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
        }
    });

    Log.d(TAG, "onCreate'd");
}


ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        Log.d(TAG, "onShutter'd");
    }
};

/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d(TAG, "onPictureTaken - raw");
    }
};

/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream outStream = null;
        try {
            // write to local sandbox file system

// outStream = CameraDemo.this.openFileOutput(String.format(“%d.jpg”,System.currentTimeMillis()),0);                 //或者写信给sdcard                 outStream = new FileOutputStream(String.format(“/ sdcard /%d .jpg”,System.currentTimeMillis()));
                outStream.write(数据);                 outStream.close();                 Log.d(TAG,“onPictureTaken - 写入字节:”+ data.length);             } catch(FileNotFoundException e){                 e.printStackTrace();             } catch(IOException e){                 e.printStackTrace();             } finally {             }             Log.d(TAG,“onPictureTaken - jpeg”);         }     };

}

1 个答案:

答案 0 :(得分:1)

如果你只是想拍照。我建议你利用意图来实现它。

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
// ...

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == Activity.RESULT_OK && requestCode == 0) {
    String result = data.toURI();
    // ...
  }
}