• Android--sos闪光灯


    Camera camera = null;
    Parameters parameters = null;

    Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    super.handleMessage(msg);
    }

    };

    // 主开启
    public void start() {
    handler.post(startThread);
    handler.post(closeThread);
    }

    // 关闭
    public void close() {
    handler.removeCallbacks(startThread);
    handler.removeCallbacks(closeThread);
    flashclose();
    camera.stopPreview();
    camera.release();
    camera = null;
    }

    private void flashopen() {//开启灯
    if (camera == null) {
    camera = Camera.open();
    }
    parameters = camera.getParameters();

    parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);

    camera.setParameters(parameters);
    camera.startPreview();
    }

    private void flashclose() {//关闭灯
    if (camera == null) {
    camera = Camera.open();
    }
    parameters = camera.getParameters();

    parameters.setFlashMode(Parameters.FLASH_MODE_OFF);

    camera.setParameters(parameters);
    }

    Runnable startThread = new Runnable() {
    // 将要执行的操作写在线程对象的run方法当中
    public void run() {
    System.out.println("updateThread");
    flashopen();
    try {
    Thread.sleep(100);
    flashclose();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    handler.post(startThread);
    }

    };

    Runnable closeThread = new Runnable() {
    // 将要执行的操作写在线程对象的run方法当中
    public void run() {
    flashclose();
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    handler.post(closeThread);
    }

    };

  • 相关阅读:
    MyEclipse Tern was unable to complete your request in time
    12.5.2 访问被覆盖的方法
    猎头、培训与咨询的价值(2)【补1】——北漂18年(93)
    12.5.1 通过 @ISA 继承
    从柯洁对战AlphaGo,看商业智能
    jquery ajax POST 例子详解
    利用组合索引优化
    Oracle range 分区表
    Perl 面向对象上
    JSON导出CSV中文乱码解决方案
  • 原文地址:https://www.cnblogs.com/laijinquan/p/5961402.html
Copyright © 2020-2023  润新知