Weird Samsung Galaxy S camera preview - android

I am using pretty much the same code as the Camera app code from Google but am getting really weird results. Inside my application and the Camera app from Google the preview gets stuck or is overlid with weird lines. The preview is usually the last thing which I see in the phone's own Camera app.
The Samsung model is I9003. The same code ran fine on I9000 which Samsung just discontinued. The code also works fine on an HTC Wildfire.
Any resolution for this?
Just noticed that after taking a photo inside my application the camera preview becomes normal. Same thing is happening in the Google camera application.

Couldn't post the answer earlier. Wasn't sure if it was the right thing to do, but now with the app running properly on around 150 devices I guess this works.
So the Android camera app in its onCreate function had the following code:
/*
* To reduce startup time, we start the preview in another thread.
* We make sure the preview is started at the end of onCreate.
*/
Thread startPreviewThread = new Thread(new Runnable() {
public void run() {
try {
mStartPreviewFail = false;
startPreview();
} catch (CameraHardwareException e) {
// In eng build, we throw the exception so that test tool
// can detect it and report it
if ("eng".equals(Build.TYPE)) {
throw new RuntimeException(e);
}
mStartPreviewFail = true;
}
}
});
startPreviewThread.start();
For some reason this did not work on GT-I9003. What I noticed was that after taking a photo the preview would come properly so there was nothing wrong with the hardware as such. I tried to retrace what was happening after a photo was taken and then compare it with the code with which the camera was first initialized. I commented out this code from onCreate. The onResume from the camera app looked like this:
if (mSurfaceHolder != null) {
// If first time initialization is not finished, put it in the
// message queue.
if (!mFirstTimeInitialized) {
mHandler.sendEmptyMessage(FIRST_TIME_INIT);
} else {
initializeSecondTime();
}
}
I changed this to:
if (!mFirstTimeInitialized) {
initializeFirstTime();
} else {
initializeSecondTime();
}
There were some other changes too, will put it up on GitHub as a separate app soon.

Related

Basic script that works fine on desktop but not loading on my Android?

I'm sorry for this basic question I'm new to this android thing...and gotta start struggling somewhere...(Please be gentle)
Have a basic sketch in processing that shows the computer's camera view in a window. Works fine on the computer. It doesn't load on my android. Just hangs and pretends to be building forever. Not sure if it has to do with camera permissions and I'm a nubbie at this so have no idea how to find out.
Any help would be greatly appreciated.
Thank you!
The code:
import processing.video.*;
Capture video;
void setup() {
size(320, 240);
video = new Capture(this, 320, 240);
video.start();
}
void captureEvent(Capture video) {
video.read();
}
void draw() {
image(video, 0, 0);
}
As it compiles this is the only apparent 'problem' but it doesn't come back as an error.
**Exception in thread "Thread-203" java.lang.NullPointerException: Cannot read the array length because "list" is null
**

android camera classic API: writing a blocking (synchronous) 'takePicture()'

I'm writing an Android app and I need to take photos. I got it mostly working very quickly using the standard tutorial. But I need to write a blocking version of "takePicture()". I've trying reworking my design so that everything is done asynchronously, but the code just ends up as spaghetti code because so many tasks are dependent on the previous task completing. And I had expected that taking a photo would take less than a second. So I think it's a reasonable goal to have a blocking (synchronous) "takePicture()".
So I created a camera in the UI thread (I'm an Android newbie but I presume that 'onCreate()' et al are called in the UI thread). I also open the camera and release it in the UI thread. I also call "takePicture()" in the UI thread. I tried polling for a "volatile byte[] jpeg;" field to become non-null, with a "while (jpeg != null) sleep(1000);" but it never gets assigned. To my surprise, my callback doesn't get called until I pass out of the "onClick()" function - 'takePicture()' doesn't seem to trigger anything, it merely queues something, as far as I can figure out.
My next attempt involved doing all the camera work (open, takePicture, release) in a "ExecutorService.newSingleThreadExecutor()" but that was also unsuccessful. Even when I did it this way (see below), the callback wasn't called until the app finished the 'onClick()'.
public byte[] takePhoto()
{
if (camera == null)
return null;
cameraThreadService.submit(new Runnable() {
#Override
public void run() {
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
jpeg = data;
}
};
camera.takePicture(null, null, mPicture);
}
});
int n = 0;
while (jpeg == null) {
sleep(1000);
if (n++ > 5)
break;
}
return jpeg;
}
Can anyone tell me how to write a blocking (synchronous) 'camera.takePicture()'?
To my surprise, my callback doesn't get called until I pass out of the "onClick()" function - 'takePicture()' doesn't seem to trigger anything, it merely queues something, as far as I can figure out.
Your onPictureTaken() is called on the main application thread. The main application thread cannot do two things at once. onPictureTaken() cannot be called until after you return from onClick().
Can anyone tell me how to write a blocking (synchronous) 'camera.takePicture()'?
That is not possible, sorry.

Camera Parameters shows wierd errors

The below flashlight code used to work properly earlier. but when I started the android IDE for next time. it shows NullPointerException Error in the specific line consist of Camera Parameters. I have tried every other conditions to trace the stack still I cant find any relevant solutions. Any help is appreciated.
I have also visited some of the related queries in the forum. one of which queries I have found useful but still It didn't work.
Google results
Flashlight.java
public void connectCameraService() {
if (camera == null) {
camera = android.hardware.Camera.open();
params = camera.getParameters(); //specific line shows NullPointerException Error.
}
}
The above method also creates Error in Override methods.
#Override
protected void onStart() {
super.onStart();
connectCameraService();
}

Android - take Picture in Service

I am trying to develop an app that would take picture when you light the screen.
I created different method to do so, and different logs. When I call the method supposed to take the picture : this is in a Service
public void photo() {
try {
cam.setPreviewDisplay(new SurfaceView(this).getHolder());
cam.startPreview();
cam.takePicture(null,null,new PhotoHandler());
Log.i("photo","end");
}catch(IOException e) {e.printStackTrace();}
}
In the logs, the "photo","end" is displayed but the logs I put in PhotoHandler are not.
I also have the "Camera" "app passed NULL Surface" log.
So I am asking you guys why is the picture not taken ? The PhotoHandler works fine, I tested it in a basic app.
Hope you'll help me !
EDIT : The camera is selected in the onCreate method of the service and gives no error.
EDIT : The problem was that my PhotoHandler wasn't called so camera.release() too. My photo() method looks like this now
public void photo() {
Camera cam=null;
try {
cam=Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
cam.setPreviewTexture(new SurfaceTexture(0));
cam.startPreview();
cam.takePicture(null,null,new PhotoHandler());
cam.stopPreview();
cam.release();
Log.i("photo","end");
}catch(IOException e) {e.printStackTrace();stopSelf();}
catch(RuntimeException re {e.printStackTrace();stopSelf();}
Now, the problem is that the takePicture method is called when the service end, must be a thread problem I assume. But what is more bothering is that the takePicture method is not always called after the service end. And when the method is called, it's called once, whereas I light the screen more than one time during the life of the service.

Android catch app crash

I was wondering if it would be possible to override a method (or something like that), that will be called when the application will crash due to some exception.
I'd like to do this, so the next time the user loggs in, it gives him/her a message that the app has crashed and that the bug will be fixed as soon as possible.
Use Thread and setDefaultUncaughtExceptionHandler().
I found a simple little solution that works perfectly:
When my app is created I do something like this:
// Ceck if the app crashed last time
if(this.getIO().readPrimitiveInternalMemoryBoolean(FileNames.SpaceDroid.CrashReport) == true) {
this.getGameLog().d(classTAG, "App crashed last time");
}
else {
this.getGameLog().d(classTAG, "App didn't crash last time");
}
// Flag the app as chrashed and when the app is exited, then mark it as false.
this.getIO().writePrimitiveInternalMemory(FileNames.SpaceDroid.CrashReport, true);
When my app is being closed, then I do something like this:
#Override
public void onDispose() {
super.onDispose();
this.getIO().writePrimitiveInternalMemory(FileNames.SpaceDroid.CrashReport, false);
}
You could use a try catch statement and log info in your logs. Also google play provides a mechanism to notify you about crashes in your apps in production.

Categories

Resources