i am using opencv 3.4.1 project to capture image i have used this link https://github.com/bluebead38/OpenCVTesseractOcr and i have capure the image perfectly now i need to run flash with on and off button while capture the image i use extend classes for camera view
Blockquote package com.bluebead38.opencvtesseractocr;
import java.util.ArrayList; import java.util.List;
import org.opencv.android.JavaCameraView;
import android.R.integer; import android.content.Context; import android.graphics.Rect; import android.graphics.RectF; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.Toast;
public class MTCameraView extends JavaCameraView implements AutoFocusCallback {
public MTCameraView(Context context, int attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public List<Camera.Size> getResolutionList() {
return mCamera.getParameters().getSupportedPreviewSizes();
}
public Camera.Size getResolution() {
Camera.Parameters params = mCamera.getParameters();
Camera.Size s = params.getPreviewSize();
return s;
}
public void setResolution(Camera.Size resolution) {
disconnectCamera();
connectCamera((int)resolution.width, (int)resolution.height);
}
public void focusOnTouch(MotionEvent event) {
Rect focusRect = calculateTapArea(event.getRawX(), event.getRawY(), 1f);
Rect meteringRect = calculateTapArea(event.getRawX(), event.getRawY(), 1.5f);
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
if (parameters.getMaxNumFocusAreas() > 0) {
List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
focusAreas.add(new Camera.Area(focusRect, 1000));
parameters.setFocusAreas(focusAreas);
}
if (parameters.getMaxNumMeteringAreas() > 0) {
List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();
meteringAreas.add(new Camera.Area(meteringRect, 1000));
parameters.setMeteringAreas(meteringAreas);
}
mCamera.setParameters(parameters);
mCamera.autoFocus(this);
}
/**
* Convert touch position x:y to {@link Camera.Area} position -1000:-1000 to 1000:1000.
*/
private Rect calculateTapArea(float x, float y, float coefficient) {
float focusAreaSize = 300;
int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();
int centerX = (int) (x / getResolution().width * 2000 - 1000);
int centerY = (int) (y / getResolution().height * 2000 - 1000);
int left = clamp(centerX - areaSize / 2, -1000, 1000);
int right = clamp(left + areaSize, -1000, 1000);
int top = clamp(centerY - areaSize / 2, -1000, 1000);
int bottom = clamp(top + areaSize, -1000, 1000);
return new Rect(left, top, right, bottom);
}
private int clamp(int x, int min, int max) {
if (x > max) {
return max;
}
if (x < min) {
return min;
}
return x;
}
public void setFocusMode (Context item, int type){
Camera.Parameters params = mCamera.getParameters();
List<String> FocusModes = params.getSupportedFocusModes();
switch (type){
case 0:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
else
Toast.makeText(item, "Auto Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 1:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
else
Toast.makeText(item, "Continuous Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 2:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_EDOF))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_EDOF);
else
Toast.makeText(item, "EDOF Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 3:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_FIXED))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
else
Toast.makeText(item, "Fixed Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 4:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_INFINITY))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
else
Toast.makeText(item, "Infinity Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 5:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_MACRO))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
else
Toast.makeText(item, "Macro Mode not supported", Toast.LENGTH_SHORT).show();
break;
}
mCamera.setParameters(params);
}
public void setFlashMode (Context item, int type){
Camera.Parameters params = mCamera.getParameters();
List<String> FlashModes = params.getSupportedFlashModes();
switch (type){
case 0:
if (FlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO))
params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
else
Toast.makeText(item, "Auto Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 1:
if (FlashModes.contains(Camera.Parameters.FLASH_MODE_OFF))
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
else
Toast.makeText(item, "Off Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 2:
if (FlashModes.contains(Camera.Parameters.FLASH_MODE_ON))
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
else
Toast.makeText(item, "On Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 3:
if (FlashModes.contains(Camera.Parameters.FLASH_MODE_RED_EYE))
params.setFlashMode(Camera.Parameters.FLASH_MODE_RED_EYE);
else
Toast.makeText(item, "Red Eye Mode not supported", Toast.LENGTH_SHORT).show();
break;
case 4:
if (FlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH))
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
else
Toast.makeText(item, "Torch Mode not supported", Toast.LENGTH_SHORT).show();
break;
}
mCamera.setParameters(params);
}
@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
}
}
and call it on button click like this
mOpenCvCameraflash1.setFlashMode(CameraView.this,1);
but it carsh the app and make stop said it can't be null my mainfest is
<uses-permission android:name="android.permission.CAMERA"
/> <uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="true" /> <uses-feature android:name="android.hardware.camera.front"/> <uses-feature android:name="android.hardware.camera.front.autofocus"/>
<uses-permission android:name="android.permission.FLASHLIGHT"
/> <uses-feature android:name="android.hardware.camera.flash"/>
<uses-permission android:name="android.permission.INTERNET"
/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>