In my application i have an image button that basically switches on and off the flash LED. The code is running fine for the first time i.e. On first Click it Switches On the LED and on the Second Click it Switches it Off. But then Nothing happens third Click onwards. I am testing this on Nexus S.
Following is the Code for the ImageButton Click Method.
public void ToggleTorch(){
final ImageButton tt = (ImageButton)findViewById(R.id.tt);
tt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
if (isFlashOn){
mycam.stopPreview();
isFlashOn = false;
} else {
mycam.startPreview();
isFlashOn = true;
}
}
});
}
From what i think, it has to do something with the SurfaceView as i think it is not being destroyed while calling stopPreview but i am not sure..
Following is the Code for the onCreate Method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Check if Flash Light is Available
Boolean has_flash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(has_flash){
setContentView(R.layout.activity_main);
SurfaceView preview = (SurfaceView)findViewById(R.id.pSv);
SurfaceHolder holder = preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
disableSleepMode();
initFlashLight();
ToggleTorch();
screenTorchOn();
} else {
setContentView(R.layout.activity_main);
disableSleepMode();
screenTorchOn();
}
}
any help would be appreciated. Thanks.
instead of calling stop preview release camera and make camera instance null. To restart camera, initialize camera again. Make two different method one for initialize camera and another to release it. This will solve your problem.
Related
I am using the camera flash for a Morse code application. I create a new camera object when the class is created. The user has a button which is used to reset and also release the camera when required (If they want to stop the light Morse sequence prematurely).
The problem is that when they hit the reset button because the activity is not created or loaded again the camera never get reinitialized - this is a problem because the method of the class that it is calling is used by another class and releases the camera when it is has completed it's function. I am not sure how to structure the code in a way that allows me to do this.
I am wondering if anyone has any advice/suggestions in how to achieve this?
//Camera object being declared
Light light;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__morse);
//Camera object being initialized
light = new Light
}
//Reset user pressed a button
public void reset(View view)
{
light.release();
}
//Releasing the camera
public void release() {
if(camera != null)
{
camera.stopPreview();
camera.release();
camera = null;
}
}
Don't initialize the camera in onCreate. Do it in onStart, and release it in onStop.
I have posted an issue to grafika, but it seems there is nobody to maintain the project now.
I want to use the CameraCaptureActivity which implemented by GLSurfaceView to switch front/back camera, as following:
public boolean switchCamera() {
releaseCamera();
mGLView.onPause();
if (mReqCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
mReqCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
} else {
mReqCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
openCamera(mReqCameraId);
mGLView.onResume();
mGLView.queueEvent(new Runnable() {
#Override
public void run() {
mRenderer.setCameraPreviewSize(mCameraPreviewWidth, mCameraPreviewHeight);
}
});
return true;
}
It can work, but the FOV has been changed when I back to the camera which first time launched. It seems the frame has been clipped.
So where did i miss when switch the front-back camera?
Thanks.
PS: I have googled, but there is little information about Android Camera with GLSurfaceView.
As #fadden's comment, i remove the setRecordingHint(true), and it works fine.
Just getting into Android programming and this is my first app. It is essentially a toggle button which turns the light on or off. The app loads fine, if I press the button it turns on fine, however if I turn it off while it is on I get "App has stopped working" message and it closes.
Can someone point where I'm going wrong? Thanks
public class MainActivity extends ActionBarActivity {
int flag = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.editText);
if (flag==0) {
flag=1;
editText.setText("ON");
Camera camera = Camera.open();
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
}
else {
if (flag == 1) {
flag = 0;
editText.setText("OFF");
Camera camera = Camera.open();
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
}
}
}
});
}
}
First off, when you get that it means you crashed. When that happens, a stack trace and exception type will be in your logcat. This will tell you how to debug.
However in this case I can tell you what happened. When you call Camera.open(), you are receiving a camera object that you own until you release it. You need to save this, and release it when your app is done with it (when it exits). So in your off code, when you call Camera.open again it returns null, because you already own the camera and it can't give it to you again.
Actually you need to account for Camera.open returning null any time you call it- another app may have requested the camera, in which case the OS can't give it to you and returns null.
Good day all, I am trying to figure out how to take a picture by pressing a button, without any preview showing up. The idea is, I want a picture to be taken and saved, but no visual preview of the photo before or afterwards. So far, I am able to get the code to take pictures and save them to the disk without any problems, but I cannot seem to do it without a surfaceview or preview.
Here is some of my code:
Main Activity:
public class MainActivity extends Activity implements OnClickListener {
private Camera cameraObject;
private ShowCamera showCamera;
private Button NOPE;
//Check if camera is avail:
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
L.m("Camera Open");
} catch (Exception e) {
L.m(e.toString());
} return object;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Opens up the camera
cameraObject = isCameraAvailiable();
//Sets the resolution for the camera (Excluded from code here)
setCameraResolution();
//Button for taking photos
NOPE = (Button) findViewById(R.id.button_capture);
NOPE.setOnClickListener(this);
//THIS SECTION OF CODE HERE I can't get it to work without it as this creates a view/ preview for the camera
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view){
cameraObject.takePicture(null, null, new PhotoHandler(getApplicationContext()));
}
public void onClick(View view) {
switch (view.getId()){
case R.id.button_capture:
snapIt(view);
}
}
}
The photo handler class:
public class PhotoHandler implements PictureCallback {
private final Context context;
public PhotoHandler(Context context) {
this.context = context;
}
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
//I removed unnecessary code here, but this is where I write to disk, which works fine.
}
}
The problem I am having is that I CANNOT actually take a photo via the camera unless I have the code about preview.addView(showCamera);.
The ShowCamera class is merely one that adds a surface view for viewing the pictures while they are being taken:
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback
Anyone have any ideas? Can this be done?
Someone asked an EXTREMELY similar question here: Taking picture without SurfaceView or without Photo Intent Activity , but without any success. I think I'm on a similar path as they were.
I have the same problem and I solve it by puting new view over the SurficeView so surficeView was not seen. I spend quite a time by searching for other solution but without success.
I want to start camera and also to automatically start recording just by clicking an app in android. I have the code to start the camera but I do not know how to start auto capture of the video. Please help.
the code I have for launching camera-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c1_main);
Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
StartActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY);
}
I found about view.performclick but do not know how to use for camera
Use MediaRecorder for this purpose. Though it will require more work but will give you much more control. Follow this link http://android-er.blogspot.tw/2011/04/start-video-recording-using.html. Since you don't reuire any button click for recording, keep a delay before starting the camera. Do it like this
myButton.setPressed(true); //you won't click the button
myButton.invalidate();
myButton.postDelayed(new Runnable() {
public void run() {
myButton.setPressed(false);
myButton.invalidate();
releaseCamera(); //release camera from preview before MediaRecorder starts
if(!prepareMediaRecorder()){
Toast.makeText(AndroidVideoCapture.this,"could not prepare MediaRecorder",Toast.LENGTH_LONG).show();
finish();
}
mediaRecorder.start();
}
},5000); //causes delay of 5 seconds befor recording starts
Ok, Make following, changes in your code.
Button play;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c1_main);
play = findViewById ( R.id.btnPlay ); // assuming you have this button in your .xml file.
play.setOnClickListener ( new OnClickListener()
{
#Override
public void onClick ( View view )
{
Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
StartActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY);
}
});
}