简体   繁体   English

相机setFocusAreas()无法运行Android 4.0

[英]Camera setFocusAreas() not working Android 4.0

I'm creating a camera app that will use a tap to focus function, much like many of the standard camera apps come with. 我正在创建一个使用水龙头来聚焦功能的相机应用程序,就像许多标准相机应用程序一样。 I first get the (x,y) coordinate of a touch event, then deliver this value to the UI. 我首先得到触摸事件的(x,y)坐标,然后将此值传递给UI。 Then, I try to set the focus area, like this: 然后,我尝试设置焦点区域,如下所示:

Rect newRect = new Rect(left,top,right, bottom);
Camera.Parameters params = mCamera.mCameraInstance.getParameters();
Camera.Area focusArea = new Camera.Area(newRect, 1000);
List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
focusAreas.add(focusArea);
params.setFocusAreas(focusAreas);

However, it appears to have no effect. 但是,它似乎没有任何效果。 So, I hardcoded some values into the rectangle (this way, I can rule out bad coordinates from the touch event). 所以,我将一些值硬编码到矩形中(这样,我可以排除触摸事件中的坏坐标)。 According to the docs , it is looking for a coordinate in the space (-1000, -1000)(upper left) to (1000, 1000)(bottom right). 根据文档 ,它正在寻找空间中的坐标(-1000,-1000)(左上)到(1000,1000)(右下)。

So I replaced my rectangle above with this one: 所以我用这个替换了上面的矩形:

//should target a 100x100 square in the center of the screen
Rect newRect = new Rect(-50,-50,50, 50);

And still, seems to have no effect. 而且,似乎没有效果。 I know my camera supports setting focus areas, because first of all, the camera app uses it successfully, and also params.getMaxNumFocusAreas() returns 1. 我知道我的相机支持设置对焦区域,因为首先,相机应用程序成功使用它,并且params.getMaxNumFocusAreas()返回1。

If anyone has used this with success in the past, please let me know!! 如果有人在过去成功使用过这个,请告诉我!

Edit: 编辑:

I found this similar question that seems to also indicate that this API simply doesn't work on Android 4.0 devices and up (im testing on 4.1.1, Galaxy S3). 我发现这个类似的问题似乎也表明这个API根本无法在Android 4.0设备上运行(我正在测试4.1.1,Galaxy S3)。 And yet, the camera apps on these devices still definitely has tap-to-focus functionality. 然而,这些设备上的相机应用程序仍然具有点对点功能。 What am I missing??? 我错过了什么?

I had this problem today :/ 我今天遇到这个问题:/

And after hours of struggling, I found the solution! 经过几个小时的挣扎,我找到了解决方案!

It's strange, but it appears that setting focus-mode to "macro" right before setting focus-areas solved the problem ;) 这很奇怪,但似乎在设置焦点区域之前将焦点模式设置为“宏”,解决了问题;)

params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
params.setFocusAreas(focusAreas);
mCamera.setParameters(params);

I have Galaxy S3 with Android 4.1.2 我有Android 4.1.2的Galaxy S3

I hope this will work for you either :) 我希望这对你有用:)

It does not work for me neither, but anyways, when setting your rect, make sure that left < right and top < bottom ! 它既不适合我,但无论如何,在设置你的矩形时,请确保left < righttop < bottom Your are setting your rect to a height of -100! 你的矩形设置为-100的高度! And, btw your not setting a square of 50x50 but 100x-100. 而且,顺便说一句,你没有设置50x50而是100x-100的正方形。

After perusing the source 4.3 source and staring at the Camera API for a few too many hours I found this function : 仔细阅读源4.3源并盯着Camera API几个小时后,我发现了这个功能

public final void autoFocus (Camera.AutoFocusCallback cb)

So you should be able to add a few lines to your code: 所以你应该能够为你的代码添加几行:

Rect newRect = new Rect(left,top,right, bottom);
Camera.Parameters params = mCamera.mCameraInstance.getParameters();
Camera.Area focusArea = new Camera.Area(newRect, 1000);
List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
focusAreas.add(focusArea);
params.setFocusAreas(focusAreas);

/**
 * ISO changes occurs passively after setting it in the cameraParams. Auto focus
 * parameters that have been set don't take affect until calling Camera.autoFocus()
 */
mCamera.autoFocus(new Camera.AutoFocusCallback()
{
    @Override
    public void onAutoFocus(boolean success, Camera camera)
    {
        Log.d(TAG, "onAutoFocus() " + success);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM