gpt4 book ai didi

java - 安卓相机: call Preview callback on Button click

转载 作者:行者123 更新时间:2023-12-01 12:37:40 25 4
gpt4 key购买 nike

正在框架布局内显示相机。我想通过单击按钮从相机获取预览帧。但我不知道如何做到这一点,因为我是一个 Android 新手。任何帮助将不胜感激。谢谢

我的代码如下:

    public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mCamera = getCameraInstance();

// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
mCamera.setDisplayOrientation(90);
preview.addView(mPreview);


findViewById(R.id.capture).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {


}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}



public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}






public void onPreviewFrame(byte[] data, Camera camera) {
try {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),
size.width, size.height, null);
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + "/out.jpg");
FileOutputStream filecon = new FileOutputStream(file);
image.compressToJpeg(
new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
filecon);
} catch (FileNotFoundException e) {
Toast toast = Toast
.makeText(getBaseContext(), e.getMessage(), 1000);
toast.show();
}
}



}

最佳答案

首先,对不起我可怜的工程师!“获取相机预览帧”这个词是否意味着您想要拍照?如果正确,您可以使用 Camera 类中的 takePicture() 方法如果没有,您可以在 onPreviewFrame 方法中使用一个标志,该标志可以通过 onClickListener 更改为 true,并且在执行操作后应更改为 false

关于java - 安卓相机: call Preview callback on Button click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25438005/

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