How I can turn on and turn off camera light on ImageButton..
This is my code
public void liBtn(View v)
{
int tur=0;
if (tur==0)
{
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
int tur=1;
}
if (tur==1)
{
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.startPreview();
int tur=0;
}
}
Any ideas?
// Create a class member variable
private boolean isOn = false;
// And use it in your method
public void liBtn(View v) {
Camera.Parameters p = camera.getParameters();
if (!isOn) {
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
} else {
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
camera.setParameters(p);
camera.startPreview();
isOn = !isOn;
}
put int tur=0; outside your method.
create a global boolean variable isFlashOn and do some thing like this
public void liBtn(View v){
if (!isFlashOn)
{
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isFlashOn =true;
}else{
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.startPreview();
isFlashOn =false;
}
}
Related
I'm working on an app that works with camera but I'm getting an error.
When the camera is active, the camera freezes when the flashlight is turned on or Error "E/Camera: Error 2". Many thanks.
public void FlashOn()
{
cam = Camera.open();
Camera.Parameters params = cam.getParameters();
if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_ON))
{
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
}
else
{
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
cam.setParameters(params);
cam.startPreview();
}
public void FlashOff()
{
Camera.Parameters params = cam.getParameters();
if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_ON))
{
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
}
else
{
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
cam.stopPreview();
cam.release();
}
I don't think that you have to use startPreview again in order to enable the flash. In an older project I did something like this to enable/disable flash:
Camera.Parameters params = camera.getParameters();
if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_ON)) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
} else {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
camera.setParameters(params);
Also, you may want to open/close the camera with AsyncTask because it is a UI blocking operation.
Update: This is how your method should look
public void FlashOn() {
Camera.Parameters params = cam.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
cam.setParameters(params);
}
For FlashOff method, things will be similar.
I am using OpenCV4Android in my project and i am using JavaCameraView. I tried to switch my phone flash light but failed. Here is my attempts.
public class Camera_View extends JavaCameraView {
private static final String TAG = "Camera View";
public boolean isFlashOn = false;
public Camera_View(Context context, AttributeSet attrs) {
super(context, attrs);
}
public List<String> getEffectList() {
return mCamera.getParameters().getSupportedColorEffects();
}
public boolean isEffectSupported() {
return (mCamera.getParameters().getColorEffect() != null);
}
public String getEffect() {
return mCamera.getParameters().getColorEffect();
}
public void setEffect(String effect) {
Camera.Parameters params = mCamera.getParameters();
params.setColorEffect(effect);
mCamera.setParameters(params);
}
public List<Size> getResolutionList() {
return mCamera.getParameters().getSupportedPreviewSizes();
}
public void setResolution(Size resolution) {
disconnectCamera();
mMaxHeight = resolution.height;
mMaxWidth = resolution.width;
connectCamera(getWidth(), getHeight());
}
public Size getResolution() {
return mCamera.getParameters().getPreviewSize();
}
public void switchFlash(boolean flash_on) {
Parameters p = mCamera.getParameters();
if (!flash_on)
p.setFlashMode(Parameters.FLASH_MODE_OFF);
else
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(p);
}
public void switchFlash() {
Camera tcamera = null;
tcamera = Camera.open();
Parameters p = tcamera.getParameters();
if (isFlashOn)
p.setFlashMode(Parameters.FLASH_MODE_OFF);
else
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
isFlashOn = !isFlashOn;
tcamera.setParameters(p);
}
public void setupCameraFlashLight(boolean flash_on) {
Camera camera = mCamera;
if (camera != null) {
Parameters params = camera.getParameters();
if (params != null) {
if (!flash_on) {
isFlashOn = false;
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.startPreview();
} else {
isFlashOn = true;
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
}
}
}
I try to switch the flash light on or off using three method. But seem likes what i found online is for nativeCameraView but i am using JavaCameraView. When i try to run the coding, i got java.lang.NullPointerException.
Can anyone help me?
I want to make the camera light flash. My code will make it flash just fine of a single button press but if I hit the button again before it has finished flashing it will crash my app. I believe I need to somehow check to see if it is still flashing before I try to start the flash again.
public class BlinkBack {
public static void blink(MainActivity ma){
if (ma.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
final Camera mCamera = Camera.open();
new CountDownTimer(5000, 100) {
int counter = 0;
public void onTick(long millisUntilFinished) {
if (counter % 2 == 0) {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
}
else {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
}
counter++;
}
public void onFinish() {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
mCamera.release();
}
}.start();
}
}
}
I found my own answer. final Camera mCamera = Camera.open(); has an unchecked RunTimeException so I just ate that exception with a try catch and it fixed it
public class BlinkBack {
public static void blink(MainActivity ma){
if (ma.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
try {
final Camera mCamera = Camera.open();
new CountDownTimer(5000, 100) {
int counter = 0;
public void onTick(long millisUntilFinished) {
if (counter % 2 == 0) {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
}
else {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
}
counter++;
}
public void onFinish() {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
mCamera.release();
}
}.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
I want to set the camera preview in reverse landscape mode,When I am trying it by manifest orientation setting the preview shows as mirror view.I have an overlay over camera preview that's why I want reverse landscape mode,I am using android 4.0.
class Preview extends SurfaceView implements SurfaceHolder.Callback { // <1>
SurfaceHolder mHolder; // <2>
public Camera camera; // <3>
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder(); // <4>
mHolder.addCallback(this); // <5>
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>
}
#Override
public void surfaceCreated(SurfaceHolder holder)
{
try {
if (camera != null) {
camera.setPreviewDisplay(holder);
}
} catch (IOException exception) {
Log.e("fd", "IOException caused by setPreviewDisplay()", exception);
}
} // Called when the holder is destroyed
#Override
public void surfaceDestroyed(SurfaceHolder holder) { // <14>
//camera.setParameters(Camera.Parameters.FLASH_MODE_OFF);
Parameters params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
camera.release();
//camera = null;
}
// Called when holder has changed
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
camera = Camera.open();
Parameters params = camera.getParameters();
List<Size> sizes = params.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
params.setSceneMode(Camera.Parameters.SCENE_MODE_LANDSCAPE);
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
params.setJpegThumbnailQuality(100);
params.setExposureCompensation(0);
params.setJpegQuality(100);
// params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
/*
*
* Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize = // .... select one of previewSizes here
parameters.setPreviewSize(previewSize.width, previewSize.height);
camera.setParameters(parameters);
camera.startPreview();
*/
//int w=0,h=0;
for(int i=0;i<sizes.size();i++)
{
if(sizes.get(i).width > size.width)
size = sizes.get(i);
}
params.setPictureSize(size.width, size.height);
//Toast.makeText(getContext(), size.width+"and"+size.height,Toast.LENGTH_SHORT).show();
camera.setParameters(params);
/* if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
{
params.set("orientation", "portrait");
camera.setDisplayOrientation(90);
}*/
camera.startPreview();
try
{
camera.setPreviewDisplay(holder);
}
catch (IOException exception)
{
camera.release();
camera = null;
}
}
}
All credits for this answer go to Jason Robinson.
Instead of using setCameraDisplayOrientation(), you can simply call camera.setDisplayOrientation(270);
Are there any way to constantly set the resolution of the photo (like 256x256) that were taken by my own camera application. Here is bits of code, Thanks.
SurfaceView cameraView;
SurfaceHolder surfaceHolder;
Camera camera;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File mFile=new File("/sdcard/Galmix");
mFile.mkdir();
cameraView = (SurfaceView)this.findViewById(R.id.CameraView);
surfaceHolder = cameraView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
cameraView.setFocusable(true);
cameraView.setFocusableInTouchMode(true);
cameraView.setClickable(true);
cameraView.setOnClickListener(this);
}
public void onClick(View v){
camera.takePicture(null, null, this);
}
#Override
public void onPictureTaken(byte[] data, Camera camera) {
try{
OutputStream imageFileOS = new FileOutputStream(String.format("/sdcard/Galmix/%d.jpg",System.currentTimeMillis()));
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
} catch(FileNotFoundException e){
Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
t.show();
} catch (IOException e) {
Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
t.show();
}
camera.startPreview();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
camera.startPreview();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try{
camera.setPreviewDisplay(holder);
Camera.Parameters parameters = camera.getParameters();
if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
parameters.set("orientation", "portrait");
camera.setDisplayOrientation(90);
}
List<String> colorEffects = parameters.getSupportedColorEffects();
Iterator<String> cei = colorEffects.iterator();
while(cei.hasNext()){
String currentEffect = cei.next();
if(currentEffect.equals(Camera.Parameters.EFFECT_SOLARIZE)){
parameters.setColorEffect(Camera.Parameters.EFFECT_SOLARIZE); break;
}
}
camera.setParameters(parameters);
} catch(IOException e){
camera.release();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}
See Camera Parameters for information.
to resolve your problem, you have to check your camera supported sizes by using this code:
Camera camera = Camera.open();
Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
Once you will se the supported Sizes, you can define them with the method :
setPictureSize(int width, int height);