安卓使用闪光灯

         刚做了一个关于闪光灯的demo,具体是当接收到短信时会有闪光灯的闪烁。原理就是用到广播,当系统向外发送action为“android.provider.Telephony.SMS_RECEIVED”的广播时,触发广播接收器的onReceive()方法,闪烁闪光灯。之前按照书上的代码,发现3.0之后的系统规定,没有界面的app无法收到系统广播,所以我又加了一个activity,但是并没有什么用,以后可以用来拓展闪烁的style。

         那么,到底怎么开启闪光灯呢?没有单纯的描述闪光灯的类,只能通过Camera类来设置。代码如下:

Camera camera=Camera.open(0)
//按理说,open的参数可以设置为空,但是那
//样的话,虚拟机会无法运行,camera为null.
Camera.Parameters parameters=camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">  </span>

        我一开始误以为Parameters.FLASH_MODE_ON是开启闪光灯,但是测试的时候发现并不能打开。查看源码之后,发现

Parameters.FLASH_MODE_ON是当按下快门时才会亮。

/**
         * Flash will not be fired.
         */
        public static final String FLASH_MODE_OFF = "off";

        /**
         * Flash will be fired automatically when required. The flash may be fired
         * during preview, auto-focus, or snapshot depending on the driver.
         */
        public static final String FLASH_MODE_AUTO = "auto";

        /**
         * Flash will always be fired during snapshot. The flash may also be
         * fired during preview or auto-focus depending on the driver.
         */
        public static final String FLASH_MODE_ON = "on";

        /**
         * Flash will be fired in red-eye reduction mode.
         */
        public static final String FLASH_MODE_RED_EYE = "red-eye";

        /**
         * Constant emission of light during preview, auto-focus and snapshot.
         * This can also be used for video recording.
         */
        public static final String FLASH_MODE_TORCH = "torch";
       那么另外几种参数:

       FLASH_MODE_OFF:关闭闪光灯

       FLASH_MODE_AUTO:在需要的时候才会开启,情况视手机硬件而定。

       FLASH_MODE_RED_EYE:在防红眼模式开启闪光灯。

       FLASH_MODE_TORCH:长时间开启,手电筒。


        需要用到的权限:

<uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <uses-feature android:name="android.hardware.flash"/>
    <uses-feature android:name="android.hardware.camera.autofocus" />
       有错误还请指正。谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值