在相机中添加相机的缩放控件

时间:2011-06-28 07:33:58

标签: android camera

我正在制作一个Camera应用程序,我在其中使用Surface View进行相机和所有其他工作。我从以下链接中获取了指南。

http://www.brighthub.com/mobile/google-android/articles/43414.aspx

现在一切正常,但问题是我无法在相机中看到任何变焦控制和亮度控制。如何设置请指导我。

提前致谢

2 个答案:

答案 0 :(得分:9)

在相机布局中添加ZoomControls。

<ZoomControls
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/surface_camera"
android:layout_centerInParent="true"
android:id="@+id/CAMERA_ZOOM_CONTROLS">
</ZoomControls>

将这些变量添加到cameraActivity

int currentZoomLevel = 0, maxZoomLevel = 0;

然后将以下代码添加到surfaceChanged方法中。

    ZoomControls zoomControls = (ZoomControls) findViewById(R.id.CAMERA_ZOOM_CONTROLS);

    if(params.isZoomSupported()){    
    maxZoomLevel = params.getMaxZoom();

    zoomControls.setIsZoomInEnabled(true);
        zoomControls.setIsZoomOutEnabled(true);

        zoomControls.setOnZoomInClickListener(new OnClickListener(){
            public void onClick(View v){
                    if(currentZoomLevel < MAX_ZOOM){
                        currentZoomLevel++;
                        camera.startSmoothZoom(currentZoomLevel);
                    }
            }
        });

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

答案 1 :(得分:2)

将这些变量添加到您的活动类

int currentZoomLevel = 0, maxZoomLevel = 0;
private Parameters params;
private Camera mCamera;
private LayoutInflater controlInflater = null;

添加到onCreate方法

controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.custom, null);
    LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.MATCH_PARENT, 
                                                        LayoutParams.MATCH_PARENT);
    this.addContentView(viewControl, layoutParamsControl);

         ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomControls1);        

         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);
         mCamera.setParameters(params);
         }
         }
         });

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

在它自己的名为“custom.xml”

的布局中添加它
    <ZoomControls
    android:id="@+id/zoomControls1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="80dp"
    android:layout_marginTop="200dp" />

如果我以任何方式帮助你,请查看我的youtube频道谢谢。 https://www.youtube.com/watch?v=MSdghmyCrOE