2012-10-24 83 views
3

我是新來的Android我已經在這裏創建了一個相機應用從頭開始在運行時創建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(); 



     } 


    } 

} 
+0

是您'myButton'的你問的按鈕?如果是這樣,它是否給你一個錯誤或不顯示? – jnthnjns

+0

它沒有顯示... –

+0

我想它與你的'setContentView()'有關,我假設你添加按鈕的'RelativeLayout佈局'在'activity_camera'佈局中,但你改變' setContentView()'到'Preview'。註釋掉該行並運行您的應用以查看它是否顯示。另外,如果按鈕是常量,那麼你不一定需要以編程方式構建它。 – jnthnjns

回答

2

試試這個作爲你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 
} 
相關問題