2014-01-22 32 views
6

我已經編寫代碼來使用自定義相機,爲此我使用SurfaceView,現在我想知道如果我需要實現放大輸入和輸出功能,我從來沒有工作過這種應用程序。SurfaceView放大使用自定義相機的輸入和輸出功能

我的代碼,我用它來創建定製的相機,見下圖:

MainActivity.java:-

public class MainActivity extends Activity { 

    private SurfaceView preview=null; 
    private SurfaceHolder previewHolder=null; 
    public Camera camera ;  
    private boolean inPreview=false; 
    ImageButton btnCapture; 


    @SuppressWarnings("deprecation") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);  

      preview=(SurfaceView)findViewById(R.id.surface);  
      previewHolder=preview.getHolder();  
      previewHolder.addCallback(surfaceCallback);  
      previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
      previewHolder.setFixedSize(getWindow().getWindowManager()  
        .getDefaultDisplay().getWidth(), getWindow().getWindowManager() 
        .getDefaultDisplay().getHeight()); 

     btnCapture = (ImageButton) findViewById(R.id.btnCapture); 

     btnCapture.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "Capture Image(s)", Toast.LENGTH_LONG).show();   
      } 
     }); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     camera=Camera.open(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     if (inPreview) { 
      camera.stopPreview(); } 
     camera.release(); 
     camera=null; 
     inPreview=false;   
    } 


    private Camera.Size getBestPreviewSize(int width, int height,Camera.Parameters parameters){ 
     Camera.Size result=null; 
     for (Camera.Size size : parameters.getSupportedPreviewSizes()) 
     { 
      if (size.width<=width && size.height<=height) 
      { 
       if (result==null) { 
        result=size; 
       } else { 
        int resultArea=result.width*result.height; 
        int newArea=size.width*size.height; 
        if (newArea>resultArea) { 
         result=size; 
        } 
       } 
      } 
     } 
     return(result); 
    } 

    SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback(){ 

     public void surfaceCreated(SurfaceHolder holder) {  
      try {   
       camera.setPreviewDisplay(previewHolder); 
      } catch (Throwable t) { 
       Log.e("PreviewDemo-surfaceCallback", 
        "Exception in setPreviewDisplay()", t); 
       Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();  
      }  
     }  

     public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) { 
      Camera.Parameters parameters=camera.getParameters();  
      Camera.Size size=getBestPreviewSize(width, height,           
        parameters);  
      if (size!=null) {  
       parameters.setPreviewSize(size.width, size.height); 
       camera.setParameters(parameters);  
       camera.startPreview();  
       inPreview=true;  
      }  
     } 

     public void surfaceDestroyed(SurfaceHolder holder) { 

     } 
    };  
} 

activity_main.xml中: -

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/splash_background" > 

    <android.view.SurfaceView 
     android:id="@+id/surface" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <ImageButton 
     android:id="@+id/btnCapture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/capture" 
     android:contentDescription="@string/app_name" /> 

    </RelativeLayout> 

回答

4

以下是縮放功能的示例代碼。請嘗試是否可以幫助您:

public class DemoActivity extends Activity { 

private FrameLayout pCameraLayout = null; // this layout contains surfaceview 
private ZoomControls zoomControls ; 
Camera mCamera=null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     enableZoom(); 
    } 

    private void enableZoom() { 
     zoomControls = new ZoomControls(this); 
     zoomControls.setIsZoomInEnabled(true); 
     zoomControls.setIsZoomOutEnabled(true); 
     zoomControls.setOnZoomInClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       zoomCamera(false); 

      } 
     }); 
     zoomControls.setOnZoomOutClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       zoomCamera(true); 
      } 
     }); 
     pCameraLayout.addView(zoomControls); 
    } 

    /** 
    * Enables zoom feature in native camera . Called from listener of the view 
    * used for zoom in and zoom out. 
    * 
    * 
    * @param zoomInOrOut "false" for zoom in and "true" for zoom out 
    */ 
    public void zoomCamera(boolean zoomInOrOut) { 
     if(mCamera!=null) { 
      Parameters parameter = mCamera.getParameters(); 

      if(parameter.isZoomSupported()) { 
       int MAX_ZOOM = parameter.getMaxZoom(); 
       int currnetZoom = parameter.getZoom(); 
        if(zoomInOrOut && (currnetZoom <MAX_ZOOM && currnetZoom >=0)) { 
         parameter.setZoom(++currnetZoom); 
        } 
        else if(!zoomInOrOut && (currnetZoom <=MAX_ZOOM && currnetZoom >0)) { 
        parameter.setZoom(--currnetZoom); 
        } 
      } 
      else 
       Toast.makeText(context, "Zoom Not Avaliable", Toast.LENGTH_LONG).show(); 

      mCamera.setParameters(parameter); 
     } 
    } 
6

我發現通過幾個環節的方式,它爲我工作,最後我已經添加代碼到我的surfaceChanged(...),我完全surfaceChanged(..)代碼看起來像下面:

聲明命名currentZoomLevel int數據類型的可變並分配作爲初始值,

int currentZoomLevel = 0; 

代碼: -

public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) { 
      params = camera.getParameters();  
      params.setFlashMode(Camera.Parameters.FLASH_MODE_ON); 
      Camera.Size size=getBestPreviewSize(width, height,           
        params);  
      if (size!=null) {  
       params.setPreviewSize(size.width, size.height); 
       camera.setParameters(params);  
       camera.startPreview();  
       inPreview=true; 
       ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomControls); 

       if (params.isZoomSupported()) { 
        final int maxZoomLevel = params.getMaxZoom(); 
        Log.i("max ZOOM ", "is " + maxZoomLevel); 
        zoomControls.setIsZoomInEnabled(true); 
        zoomControls.setIsZoomOutEnabled(true); 

        zoomControls.setOnZoomInClickListener(new OnClickListener(){ 
         public void onClick(View v){ 
          if(currentZoomLevel < maxZoomLevel){ 
           currentZoomLevel++; 
           //mCamera.startSmoothZoom(currentZoomLevel); 
           params.setZoom(currentZoomLevel); 
           camera.setParameters(params); 
          } 
         } 
        }); 

        zoomControls.setOnZoomOutClickListener(new OnClickListener(){ 
         public void onClick(View v){ 
          if(currentZoomLevel > 0){ 
           currentZoomLevel--; 
           params.setZoom(currentZoomLevel); 
           camera.setParameters(params); 
          } 
         } 
        });  
       } 
       else 
        zoomControls.setVisibility(View.GONE); 
       }  
      } 

XML: -

<ZoomControls 
    android:id="@+id/zoomControls" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" /> 

我把我的答案,只是爲了更clearity和觀衆將來使用..

+0

加上一個形式我的身邊!真正節省了我的時間...... – Radhey

相關問題