nisrulz / qreader

:white_square_button: [Android Library] Read QR codes using google's mobile vision api, but without the hassle
http://nisrulz.github.io/qreader
Apache License 2.0
371 stars 108 forks source link

getCamera() or openFlash() #53

Closed SmallWeiChen closed 3 years ago

SmallWeiChen commented 6 years ago

你好(Hello): 我是來自台灣(Taiwan)的開發者(Developer) 非常抱歉英文不好所以使用中文 您寫的 QREader 掃描非常好用(QREader very good) 但我的專案需要打開閃光燈(open flash)

import android.hardware.Camera;
public void openFlash() {
    Camera camera = Camera.open();
    Camera.Parameters parameters = camera.getParameters();
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(parameters);
}

為此希望能在 QREader.class 加入 getCamera() 或 openFlash() / closeFlash() (QREader.class add method to getCamera() or openFlash() and closeFlash())

CameraSource cameraSource = new CameraSource.Builder(...)...
public Camera getCamera() {
    Field[] declaredFields = CameraSource.class.getDeclaredFields();
    for (Field field : declaredFields) {
        if (field.getType() == Camera.class) {
            field.setAccessible(true);
            try {
                Camera camera = (Camera) field.get(cameraSource);
                if (camera != null) {
                    return camera;
                }
                return null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        }
    }
    return null;
}

public void openFlash() {
    Camera camera = getCamera();
    Camera.Parameters parameters = camera.getParameters();
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(parameters);
}

public void closeFlash() {
    Camera camera = getCamera();
    Camera.Parameters parameters = camera.getParameters();
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(parameters);
}

謝謝您(Thank you)

nisrulz commented 3 years ago

Closing as issue is outdated. Please open a new issue if the issue still persists.