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

标签 android button camera surfaceview

我是 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/

相关文章:

android - 构建 Ionic 应用程序时出错 : Failed to execute aapt (CordovaLib:processDebugResources)

android - 如何用两个不同的对象填充 ExpandableListView

单击 JavaScript 按钮导航到另一个 HTML 页面

ios - 类型 'CMSampleBuffer' 的值没有成员 'imageBuffer'

java - Camera在Libgdx中以及与视口(viewport)一起工作的方式

android - 从 iframe 加载 Chrome Android Intent

android - 如果应用程序尚未运行,广播接收器不会收到 ACTION_BOOT_COMPLETED?

android - 单击按钮开始新线程

xcode - UIButton Swift 上的多行标签

ios - 在 Swift 3 中使用 AVFoundation 设置图片方向