gpt4 book ai didi

android - 如何在onclick 事件中访问camera.java?

转载 作者:行者123 更新时间:2023-11-29 02:17:01 25 4
gpt4 key购买 nike

我正在制作一个点击按钮拍照的应用

我有 camera.java 可以操作相机和拍照

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

public void onClick(DialogInterface arg0, int arg1) {

setContentView(R.layout.startcamera);

            }

相机.java

打包 neuro.com;

导入java.io.FileNotFoundException; 导入 java.io.FileOutputStream; 导入 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)类 CameraDemo 扩展 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(); } 最后 { } Log.d(TAG, "onPictureTaken - jpeg"); } };

最佳答案

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

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();
// ...
}
}

关于android - 如何在onclick 事件中访问camera.java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com