Android Custom Camera Zoom Not Working

2019-02-19 10:49发布

问题:

I have seen several other questions on this subject but none seem to solve my problem. I have a custom camera app that is working fine, everything but the zoom buttons. this is my code using SDK min 8 target 14:

@Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            if (isPreviewing){ 
                camera.stopPreview(); 
            }

            Camera.Parameters p = camera.getParameters();
            p.setPreviewSize(sizes.get(0).width, sizes.get(0).height);
            p.setColorEffect(effect); 

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

            if (p.isZoomSupported()) {
                maxZoomLevel = p.getMaxZoom();
                Toast.makeText(PictureTaker.this, String.valueOf(maxZoomLevel),Toast.LENGTH_LONG).show();
                zoomControls.setIsZoomInEnabled(true);
                zoomControls.setIsZoomOutEnabled(true);

                zoomControls.setOnZoomInClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        if (currentZoomLevel < maxZoomLevel) {
                            currentZoomLevel++;
                            camera.startSmoothZoom(currentZoomLevel);
                            //Toast.makeText(PictureTaker.this, String.valueOf(currentZoomLevel),Toast.LENGTH_LONG).show();
                        }
                    }
                });

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

            camera.setParameters(p); 

            try {
                camera.setPreviewDisplay(holder); 
            } // end try
            catch (IOException e) {
                Log.v(TAG, e.toString());
            } // end catch

            camera.startPreview(); // begin the preview
            isPreviewing = true;
        }

The setColorEffect is coming from the options menu and works perfectly. I know isZoomSupported and getMaxZoom are working because the Toast displays a "59" when the code runs, but the zoom buttons do nothing. This is the zoomControl from the XML

<ZoomControls
 android:id="@+id/zoomControls"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="17dp"
 android:baselineAligned="false"
 android:gravity="center_horizontal"
 android:orientation="horizontal" />

I have all the necessary permissions in the Manifest and no errors are showing in LogCat. Not sure what I'm doing wrong. I added a second Toast to report if the currentZoomLevel is being changed when the button is pressed and it shows the value getting incremented by one each time. I also tried not using startSmoothZoom and just setting the zoom with

p.setZoom(currentZoomLevel); or p.setZoomLevel(15); 

and neither one works either. My phone, HTC Incredible does have a perfectly working zoom on its native camera app. If I comment out the zoomControl parts of the code, everything works fine and all other features of the custom camera work fine even with the zoomControl code in there, it just doesn't zoom.

回答1:

Figured it out and maybe this can help some of the other people who have been having similar problems. It was the smoothzoom. Guess my HTC doesn't support this.

@Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            if (isPreviewing){ 
                camera.stopPreview(); 
            }

            p = camera.getParameters();
            p.setPreviewSize(sizes.get(0).width, sizes.get(0).height);
            p.setColorEffect(effect); 



            if (p.isZoomSupported() && p.isSmoothZoomSupported()) {
                //most phones
                maxZoomLevel = p.getMaxZoom();

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

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

                        }
                    }
                });

                zoomControls.setOnZoomOutClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        if (currentZoomLevel > 0) {
                            currentZoomLevel--;
                            camera.startSmoothZoom(currentZoomLevel);
                        }
                    }
                });
            } else if (p.isZoomSupported() && !p.isSmoothZoomSupported()){
                //stupid HTC phones
                maxZoomLevel = p.getMaxZoom();

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

                zoomControls.setOnZoomInClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        if (currentZoomLevel < maxZoomLevel) {
                            currentZoomLevel++;
                            p.setZoom(currentZoomLevel);
                            camera.setParameters(p);

                        }
                    }
                });

                zoomControls.setOnZoomOutClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        if (currentZoomLevel > 0) {
                            currentZoomLevel--;
                            p.setZoom(currentZoomLevel);
                            camera.setParameters(p);
                        }
                    }
                });
            }else{
                //no zoom on phone
                zoomControls.setVisibility(View.GONE);
            }

            camera.setParameters(p); 

            try {
                camera.setPreviewDisplay(holder); 
            } // end try
            catch (IOException e) {
                Log.v(TAG, e.toString());
            } // end catch

            camera.startPreview(); // begin the preview
            isPreviewing = true;
        } // end method surfaceChanged

This lets my HTC zoom in steps. The key was setting the camera's parameters after each button click. You could probably set the currentZoomLevel to any number you want, based on the maxZoomLevel, (my HTC is 59 but my Droid 4 is only 15), during the clicks of the ZoomControls to make the device zoom in and out faster. Might be a tidier way to code this, should probably put some checks in to make sure maxZoomSize doesn't return a NULL or something, but it's working on multiple devices.



回答2:

Its simple forget about startSmoothZoom() method

follow this:

Camera.Parameters params = camera.getParameters();
params.setZoom(zoom_value);
camera.setParameters(params);


回答3:

params.setZoom(currentZoomLevel); camera.setParameters(params); code also work at my Samsung G3 phone.