Android - Camera + ZoomControl

摘要:Android - Camera + ZoomControl

我想要在Camera 加上能夠放大縮小的功能,

所以找了網路解,還好,有一個stackoverflow有寫這個問題

SurfaceView Zoom IN and OUT functionality using Custom Camera

http://stackoverflow.com/questions/21280196/surfaceview-zoom-in-and-out-functionality-using-custom-camera

 

雖然不是我想要的,還算是一種學習。

將他另外整理到我的Demo app,就如下呈現


    
    private ZoomControls zoomControls ;
    private RelativeLayout cameraLayout; 
    
    public void initViews() {
        cameraLayout = (RelativeLayout)this.findViewById(R.id.cameraLayout);
    }
    
    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);
            }
        });
        cameraLayout.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(camera!=null) {
            Parameters parameter = camera.getParameters();

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

            camera.setParameters(parameter);
        }
    } 

我想原理就是

先判斷camera 是否支援放大縮小


            Parameters parameter = camera.getParameters();

            if(parameter.isZoomSupported()) {

            }

再重設

parameter.setZoom(++currnetZoom);

再重設

camera.setParameters(parameter);

 

就會影響SurfaceView的畫面

 

使用的ZoomControl,就是常在WebView 看到的有+,-的那種Control。

不過大概知道是用什麼方式去放大縮小了。