简体   繁体   English

Galaxy Nexus上的LED手电筒可通过什么API控制?

[英]LED flashlight on Galaxy Nexus controllable by what API?

So many LED flashlight API questions for Android. 如此多的LED手电筒API问题适用于Android。 I'm afraid to ask yet another, but here goes.. 我不敢问另一个,但是这里......

Using the tried and true FLASH_MODE_TORCH I am able to achieve satisfaction with my Samsung Galaxy SII and get the LED flash turned on. 使用经过验证的真实FLASH_MODE_TORCH,我可以满足我的三星Galaxy SII并启用LED闪光灯。 On my friend's Galaxy Nexus, no such luck. 在我朋友的Galaxy Nexus上,没有这样的运气。 Nor on my other friend's Droid X. 也不是我的另一个朋友的Droid X.

I'm noticing for a not insignificant number of devices specific native IOCTL calls seem to be required. 我注意到似乎需要特定的本机IOCTL调用的设备数量不是很少。 Is this the case for the Galaxy Nexus? 这是Galaxy Nexus的情况吗? How do I find a reference to program it? 我如何找到编程参考?

I am doing the standard FLASH_MODE_TORCH/"flash-mode"="torch", startPreview() chain. 我正在做标准的FLASH_MODE_TORCH /“flash-mode”=“torch”,startPreview()链。

Kind of disappointing that this seemingly standard API doesn't appear to be so universal after all. 令人失望的是,这个看似标准的API似乎并不是那么普遍。

What I found out is that some devices need a SurfaceView to turn on the LED. 我发现有些设备需要SurfaceView来打开LED。

SurfaceView preview = (SurfaceView) findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
Camera mCamera = Camera.open();
mCamera.setPreviewDisplay(mHolder);

// Turn on LED  
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);      
mCamera.startPreview();

...

// Turn off LED
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
mCamera.release();

Your activity needs to implement SurfaceHolder.Callback: 您的活动需要实现SurfaceHolder.Callback:

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {}

public void surfaceCreated(SurfaceHolder holder) {
    mHolder = holder;
    mCamera.setPreviewDisplay(mHolder);
}

public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.stopPreview();
    mHolder = null;
}

The surfaceview has to be visible, android:visibility="invisible" or a height and width of 0 won't work. 必须显示surfaceview, android:visibility="invisible"或高度和宽度为0将不起作用。 Unfortunately I didn't find a good solution to hide it, so I just gave it a size of 1x1dip and positioned it underneath a button.. 不幸的是我没有找到一个很好的解决方案来隐藏它,所以我只给它一个1x1dip的大小并将它放在一个按钮下面..

**(To expand on above paragraph [Don't have enough rep to reply, but felt it was useful]) Somewhere in the XML of your current Content View, you want: **(扩展上面的段落[没有足够的代表回复,但觉得它很有用])在当前内容视图的XML中的某个地方,您需要:

<SurfaceView
    android:id="@+id/PREVIEW"
    android:layout_width="1dip"
    android:layout_height="1dip"/>

If you have it inside a RelativeLayout (preferred), you can also do alignParentLeft/Bottom to tuck it in a corner. 如果你在RelativeLayout(首选)中有它,你也可以使用alignParentLeft / Bottom将它塞进一个角落里。 This method works for my Galaxy Nexus, but it's a known problem (phone-side) for Droid X (with the .621 update, anyway), and doesn't work on mine. 这种方法适用于我的Galaxy Nexus,但它是Droid X的一个已知问题(电话端)(无论如何都有.621更新),并且不适用于我的。 Great answer, timosch! 很棒的回答,蒂莫什!

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

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