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

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

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

如何在下面的事件中调用它?

public void onClick(DialogInterface arg0,int arg1){

的setContentView(R.layout.startcamera);

            }

相机.java

包neuro.com;

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

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

public class CameraDemo extends Activity {private static final String TAG =“CameraDemo”; 摄像机; 预览预览; 按钮buttonClick;

/** 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”); }};

}


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

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();
    // ...
  }
}
链接地址: http://www.djcxy.com/p/29573.html

上一篇: how to access camera.java in on cick event?

下一篇: How to add manifest permission to android application?