2

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.

4

3 回答 3

5

想通了,也许这可以帮助其他一些遇到类似问题的人。这是平滑缩放。猜猜我的HTC不支持这个。

@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

这可以让我的 HTC 逐步放大。关键是在每次单击按钮后设置相机的参数。您可以根据 maxZoomLevel 将 currentZoomLevel 设置为您想要的任何数字(我的 HTC 是 59,但我的 Droid 4 只有 15),在单击 ZoomControls 以使设备放大和缩小更快。可能是一种更简洁的编码方式,应该进行一些检查以确保 maxZoomSize 不返回 NULL 或其他内容,但它可以在多个设备上运行。

于 2013-05-11T10:43:04.787 回答
1

它简单地忘记了 startSmoothZoom() 方法

按照这个:

Camera.Parameters params = camera.getParameters();
params.setZoom(zoom_value);
camera.setParameters(params);
于 2017-02-26T19:55:11.763 回答
0

params.setZoom(currentZoomLevel); camera.setParameters(params); 代码也适用于我的三星 G3 手机。

于 2014-05-03T16:48:44.520 回答