Android Custom Camera Zoom Not Working

前端 未结 3 1771
抹茶落季
抹茶落季 2021-01-15 03:48

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

3条回答
  •  孤城傲影
    2021-01-15 04:00

    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.

提交回复
热议问题