gpt4 book ai didi

Android 在运行时将按钮放在 surfaceview 上以捕获图像

转载 作者:太空狗 更新时间:2023-10-29 13:33:36 28 4
gpt4 key购买 nike

我是 android 的新手 我在这里从头开始创建了一个相机应用程序 我在运行时创建了 surfaceview 我想在它上面创建一个按钮我该怎么做???

这是我的代码相机显示完美想要在它上面添加一个按钮来捕获图像

package com.example.urvey.tool;
import android.os.Bundle;
import java.lang.*;
import android.view.MenuItem;
import android.widget.*;
import android.support.v4.app.NavUtils;
import android.text.Layout;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import android.content.Intent;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.*;



@SuppressLint({ "SdCardPath", "NewApi" }) public class Camera_screen extends Activity {
//AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
Preview P = new Preview(this);
setContentView(P);

Button myButton = new Button(this);
myButton.setText("Press Me");
System.out.println("halyu");
RelativeLayout layout = (RelativeLayout) findViewById(R.id.cam_out);
layout.addView(myButton);



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_camera, menu);
return true;
}



@SuppressLint("SdCardPath") class Preview extends SurfaceView implements SurfaceHolder.Callback {

SurfaceHolder mHolder;
Camera mCamera;



public Preview(Context context) {

super(context);

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = this.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
System.out.println("constructor");
}


@SuppressLint({ "SdCardPath", "SdCardPath" }) public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.

try {

mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);

} catch (IOException exception) {
mCamera.release();
mCamera = null;



// TODO: add more exception handling logic here
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}

public void previewCamera()
{
try
{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch(Exception e)
{

}
}


@SuppressLint("NewApi") public Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters){
Camera.Size bestSize = null;
List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();

bestSize = sizeList.get(0);

for(int i = 1; i < sizeList.size(); i++){
if((sizeList.get(i).width * sizeList.get(i).height) >
(bestSize.width * bestSize.height)){
bestSize = sizeList.get(i);
}
}

return bestSize;
}



@SuppressLint("NewApi") public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.



Camera.Parameters parameters = mCamera.getParameters();
Camera.Size myBestSize = getBestPreviewSize(3, 4, parameters);
/* List<Size> sizes = parameters.getSupportedPreviewSizes();
Size size = sizes.get(0);*/
if(myBestSize != null){
mCamera.setDisplayOrientation(90);
parameters.setPreviewSize(myBestSize.width, myBestSize.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}






mCamera.startPreview();



}


}

}

最佳答案

试试用这个代替你的onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// Preview P = new Preview(this);
// setContentView(P);

Button myButton = new Button(this);
myButton.setText("Press Me");
myButton.setOnClickListener(this);

RelativeLayout layout = (RelativeLayout) findViewById(R.id.cam_out);
layout.addView(myButton);

}

public void onClick(View v) {
Log.i("onClick", "myButton");
// Process the Image, etc
}

关于Android 在运行时将按钮放在 surfaceview 上以捕获图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13055632/

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