Android Question CUSTOM HARDWARE FLASH LIGHTS

jahswant

Well-Known Member
Licensed User
Longtime User
Hi all i have a custom hardware with 2 Flash lights that can be accessed individually. The manufacturer have provided the java methods to access these camara lights and set their intensities individually.The problem is that i have been using the camara lib while ignoring these lights.Now my project requests me to use these lights to ameliorate the image quality.I wrapped the java methods correctly but they block the camara lib and i get some exception like i cannot connect to the camara service.My question is : can i use these methods while using the camara. If yes HOW ? It seems like you cannot preview the camara while these lights are ON because they lock the CAMARA lib. Any suggestion will be much appreciated.
 

jahswant

Well-Known Member
Licensed User
Longtime User
Do you have jar / aar ?
I was trying since then to make the methods work with the reflection library using camaraEx example . but no way ... Help please

This is my try

B4X:
Sub createMorphoTabletReflectionMethodsSetFlashTorch
   
    r.target = parameters
   
    r.GetMethod("setFlashTorch",Array As String("java.lang.boolean"))
   
    CommitParameters
   
End Sub



Sub initLed

    Dim jo As JavaObject = nativeCam
   
    r.Target = jo
   
    r.InvokeMethod(jo,r.RunMethod("setFlashTorch"),Array(False))
   
    r.InvokeMethod(jo,r.RunMethod("setFlashValue"),Array(valueFlash1,valueFlash2))
   
    r.InvokeMethod(jo,r.RunMethod("setFlashTorch"),Array(True))

   
End Sub

Public Sub setLedPower(Device As String ,Value As Int )
   
    Dim jo As JavaObject = nativeCam
   
    r.Target = jo

If Device = led1  Then
    valueFlash2 = Value
    Else If Device = led2  Then
    valueFlash1 = Value
   End If
  
   r.InvokeMethod(jo,r.RunMethod("setFlashValue"),Array(valueFlash1,valueFlash2))
 
    r.InvokeMethod(jo,r.RunMethod("setFlashTorch"),Array(True))
End Sub

These are original methods :

B4X:
package com.morpho.camera_led_demo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

/**
* Created by g580872 on 08/06/2016.
*/
public class CameraLedActivity extends Activity {
    private int valueFlash1;

    private int valueFlash2;

    private Camera camera;

    private Parameters parameters;

    /** To enable/disable power of LED1 , must use "led1". */
    private static final String led1 = "led1";

    /** To enable/disable power of LED2 , must use "led2". */
    private static final String led2 = "led2";
    /** Declaring SeekBar to cast / Initialize from XML UI. */
    private SeekBar levelLed1 = null;

    private TextView textLed1 = null;

    /** Declaring SeekBar to cast / Initialize from XML UI. */
    private SeekBar levelLed2 = null;

    private TextView textLed2 = null;


    private Method MTAB_SetFlashValues = null;
    private Method MTAB_SetFlashTorch = null;
    int progressChanged = 0;
    int progressChanged2 = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createMorphoTabletReflectionMethods();
        createMorphoTabletReflectionMethodsSetFlashTorch();
        setContentView(R.layout.activity_camera_led);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setBackgroundDrawable(
                getResources().getDrawable(R.drawable.actionbar_bg_gradient));



        textLed1 = (TextView) findViewById(R.id.flashText1);
        levelLed1 = (SeekBar) findViewById(R.id.seekBar1);
        textLed2 = (TextView) findViewById(R.id.flashText2);
        levelLed2 = (SeekBar) findViewById(R.id.seekBar2);
        if (savedInstanceState != null) {
            valueFlash1 = savedInstanceState.getInt("levelLed2");
            valueFlash2 = savedInstanceState.getInt("levelLed1");
        }
        initLed();

        levelLed1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {


            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                progressChanged = progress;
                textLed1.setText("Value of Led1: " + progress);
                setLedPower(led1, progressChanged);

            }

            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        levelLed2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                progressChanged2 = progress;
                textLed2.setText("Value of Led2: " + progress);
                setLedPower(led2, progressChanged2);
            }

            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }



    @SuppressWarnings("rawtypes")
    private void createMorphoTabletReflectionMethods() {
        if (camera == null) {
            camera = Camera.open();
        }

        Class<? extends Camera.Parameters> c = camera.getParameters()
                .getClass();
        Class[] cArg1 = { int.class, int.class };

        try {
            MTAB_SetFlashValues = c.getMethod("setFlashValue", cArg1);

        } catch (NoSuchMethodException e) {

            e.printStackTrace();
        } catch (IllegalArgumentException e) {

            e.printStackTrace();
        }
    }

    private void createMorphoTabletReflectionMethodsSetFlashTorch() {
        if (camera == null) {
            camera = Camera.open();
        }

        Class<? extends Camera.Parameters> c = camera.getParameters()
                .getClass();
        Class[] cArg1 = { boolean.class};

        try {
            MTAB_SetFlashTorch = c.getMethod("setFlashTorch", cArg1);

        } catch (NoSuchMethodException e) {

            e.printStackTrace();
        } catch (IllegalArgumentException e) {

            e.printStackTrace();
        }
    }

    public void initLed() {
        if (camera == null) {
            camera = Camera.open();
        }
        parameters = camera.getParameters();
        try {
            MTAB_SetFlashTorch.invoke(parameters,false);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
       // parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        //camera.setParameters(parameters);
        try {
            MTAB_SetFlashValues.invoke(parameters,valueFlash1, valueFlash2);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        try {
            MTAB_SetFlashTorch.invoke(parameters,true);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        camera.setParameters(parameters);
    }

    public void setLedPower(String device, int value) {
        parameters = camera.getParameters();
        //parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        //camera.setParameters(parameters);

        if (device.equals(led1)) {
            valueFlash2 = value;
        } else {
            valueFlash1 = value;
        }
        try {
            MTAB_SetFlashValues.invoke(parameters,valueFlash1, valueFlash2);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            MTAB_SetFlashTorch.invoke(parameters,true);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        camera.setParameters(parameters);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                finish();
            }
            break;

            default:
                break;
        }

        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (camera == null) {
            camera = Camera.open();
        }
    }

    @Override
    protected void onPause() {
        if (camera != null) {
            camera.stopPreview();
            camera.release();
            camera = null;
        }
        super.onPause();
    }

    protected void onSaveInstanceState(Bundle saveInstanceState) {
        super.onSaveInstanceState(saveInstanceState);
        saveInstanceState.putInt("levelLed1", levelLed1.getProgress());
        saveInstanceState.putInt("levelLed2", levelLed2.getProgress());
    }

    public void onRestoreInstanceState(Bundle savedInstanceState) {
        levelLed1.setProgress(savedInstanceState.getInt("levelLed1"));
        levelLed2.setProgress(savedInstanceState.getInt("levelLed2"));
    }
}
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
There may be other issues, but one of them is that you are trying to invoque methods over nativeCam when they should be invoqued over camera.Parameters.

B4X:
Sub initLed
   Dim jo As JavaObject = nativeCam
   Dim jo2 as JavaObject = jo.RunMethod("getParameters",Null)
   jo2.RunMethod("setFlashTorch",Array(False))
   jo2.RunMethod("setFlashValue",Array(valueFlash1,valueFlash2))
   jo2.RunMethod("setFlashTorch",Array(True))
   jo.RunMethod("setParameters",Array(jo2))
End Sub

Public Sub setLedPower(Device AsString ,Value As Int )
   Dim jo As JavaObject = nativeCam
   Dim jo2 as JavaObject = jo.RunMethod("getParameters",Null)
   If Device = led1 Then
      valueFlash2 = Value
   Else If Device = led2 Then
      valueFlash1 = Value
   End If
   jo2.RunMethod("setFlashValue",Array(valueFlash1,valueFlash2))
   jo2.RunMethod("setFlashTorch",Array(True))
   jo.RunMethod("setParameters",Array(jo2))
End Sub
 
Upvote 0
Top