Android的自定义相机变焦不工作 [英] Android Custom Camera Zoom Not Working

查看:240
本文介绍了Android的自定义相机变焦不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了关于这个问题的其他一些问题,但似乎没有解决我的问题。我有一个自定义的摄像头应用程序,工作正常,一切,但变焦按钮。这是我使用SDK分钟code 8目标14:

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;
        }

该setColorEffect是从选项菜单来了,完美的作品。我知道isZoomSupported和getMaxZoom正因为吐司显示59的code运行时,而变焦按钮无可奈何。这是zoomControl从XML

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" />

我在清单所有必要的权限,并没有错误都出现在LogCat中。不知道我在做什么错。我添加了第二个吐司如果currentZoomLevel时按钮是pressed被更改报告,它显示了一个每个时间的价值得到增加。我也尝试过不使用startSmoothZoom,只设置了变焦

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); 

和没有一个作品无论是。我的手机,HTC不可思议确实有它的原生相机应用程序完美的工作变焦。如果我注释掉的code中的zoomControl部分,一切工作正常和专用相机做工细腻,即使在zoomControl code在那里的所有其他功能,它只是不放大。

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.

推荐答案

想通了,也许这可以帮助一些其他人谁一直有类似的问题。这是smoothzoom。想我的HTC不支持这一点。

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

这让我的HTC放大步骤。关键是以后每次按一下按钮设置相机的参数。你也许可以设置currentZoomLevel你想要的任何数量的基础上,maxZoomLevel,(我的HTC是59,但我的Droid 4只有15),中了ZoomControls的点击,使设备放大和缩小速度更快。可能是一个整洁的方式来$ C C这个$,也许应该把一些支票,以确保maxZoomSize不返回NULL或东西,但它的工作在多个设备上。

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.

这篇关于Android的自定义相机变焦不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆