Reputation: 5904
I'm trying to switch between three methods to turn on/off and automathic the flash light in my camera app.. I would switch using only one button like in google camera app. Tap first and you turn off, second time you will turn it on and third time you will set it in automathic. I created the button and i made this method to change the icon:
private void updateFlashOnScreenIndicator(String value) {
ImageButton switchFlash = (ImageButton) findViewById(R.id.flash_on_off);
if (switchFlash == null) {
return;
}
if (value == null || Parameters.FLASH_MODE_OFF.equals(value)) {
switchFlash.setImageResource(R.drawable.ic_flash_off_white_24dp);
} else {
if (Parameters.FLASH_MODE_AUTO.equals(value)) {
switchFlash.setImageResource(R.drawable.ic_flash_auto_white_24dp);
} else if (Parameters.FLASH_MODE_ON.equals(value)) {
switchFlash.setImageResource(R.drawable.ic_flash_on_white_24dp);
} else {
switchFlash.setImageResource(R.drawable.ic_flash_off_white_24dp);
}
}
}
it works basically... infact, if i try to create a method (that is called in the onClick of my button) just writing:
public void flashToggle(View view) {
updateFlashOnScreenIndicator("on");
}
it changes the icon and set with the flash on.. But of course it not set really the flash on! How can i switch between theese three modality in my on click?
EDIT: I found this class and onClick in my button now i can switch between the states and icons changes correctly...but still nothing happen to the flash..it doesn't change.. it's always off
public class FlashButton extends ImageButton {
private Camera.Parameters mParameters;
private Camera camera;
public enum FlashEnum {
AUTOMATIC, ON, OFF
}
public interface FlashListener {
void onAutomatic();
void onOn();
void onOff();
}
private FlashEnum mState;
private FlashListener mFlashListener;
public FlashButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
int next = ((mState.ordinal() + 1) % FlashEnum.values().length);
setState(FlashEnum.values()[next]);
performFlashClick();
}
});
//Sets initial state
setState(FlashEnum.AUTOMATIC);
}
private void performFlashClick() {
Camera.Parameters parameters = CameraController1.getParameters();
if(mFlashListener == null)return;
switch (mState) {
case AUTOMATIC:
mFlashListener.onAutomatic();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
Log.d("Falsh", "Auto");
break;
case ON:
mFlashListener.onOn();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
Log.d("Falsh", "On");
break;
case OFF:
mFlashListener.onOff();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
Log.d("Falsh", "Off");
break;
}
}
private void createDrawableState() {
switch (mState) {
case AUTOMATIC:
setImageResource(R.drawable.ic_flash_auto_white_24dp);
break;
case ON:
setImageResource(R.drawable.ic_flash_on_white_24dp);
break;
case OFF:
setImageResource(R.drawable.ic_flash_off_white_24dp);
break;
}
}
public FlashEnum getState() {
return mState;
}
public void setState(FlashEnum state) {
if(state == null)return;
this.mState = state;
createDrawableState();
}
public FlashListener getFlashListener() {
return mFlashListener;
}
public void setFlashListener(FlashListener flashListener) {
this.mFlashListener = flashListener;
}
}
The button now is:
<net.cc.mycameraapp.FlashButton
android:id="@+id/flash_on_off"
android:background="@null"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:layout_marginTop="4dp"
android:src="@drawable/ic_flash_off_white_24dp"
android:padding="14dp"
android:layout_centerHorizontal="true">
</net.cc.mycameraapp.FlashButton>
Upvotes: 1
Views: 2589
Reputation: 318
make this one global
Camera.Parameters parameters;
Icon flash;
flash = (Icon) findViewById(R.id.flash);
make a seperate parameters that is local and set it each time when flash is clicked. I have maintained the text c i and N for FLASH AUTO, FLASH ON and FLASH OFF respectively.
flash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flash.getText().equals("c")) {
flash.setText("N");
Camera.Parameters mParameters;
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
mCamera.setParameters(mParameters);
} else
if (flash.getText().equals("N")) {
flash.setText("i");
Camera.Parameters mParameters;
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParameters);
} else if (flash.getText().equals("i")) {
flash.setText("c");
Camera.Parameters mParameters;
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
mCamera.setParameters(mParameters);
}
}
});
And inside the surfacecreated put it as below
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
mCamera = Camera.open();
parameters = mCamera.getParameters();
if (flash.getText().equals("c")) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
} else if (flash.getText().equals("N")) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
} else if (flash.getText().equals("i")) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
mCamera.setParameters(parameters);
setCameraDisplayOrientation(this, Camera.CameraInfo.CAMERA_FACING_BACK, mCamera);
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (RuntimeException e) {
System.err.println("camera0 :" + e);
return;
} catch (IOException e) {
e.printStackTrace();
}
}
Upvotes: 4