Je suis en train de développer une application Android qui prend des photos toutes les n secondes mais je n'arrive pas à afficher de preview de ma caméra sur mon téléphone.
Le preview marchait parfaitement lorsque je générais les prises de vues après l'appui sur un bouton mais plus depuis que c'est automatisé.
L'application plante tout simplement lorsque j'essaye d'afficher une preview.
J'ai essayer de placer mon code à plusieurs endroits (onResume; onCreate...) mais même résultat à chaque fois. J'ai aussi essayé de faire un rafraichissement de ma preview après chaque prise de vue, mais même soucis au final.
Logcat
03-29 10:51:16.575: D/AndroidRuntime(660): Shutting down VM
03-29 10:51:16.575: W/dalvikvm(660): threadid=1: thread exiting with uncaught exception (group=0x40c571f8)
03-29 10:51:16.575: E/AndroidRuntime(660): FATAL EXCEPTION: main
03-29 10:51:16.575: E/AndroidRuntime(660): java.lang.RuntimeException: Unable to start activity
MainActivity
class ImageSender implements Runnable{
final private int limit = 5;
int current = 0;
boolean alive=true;
synchronized void received(){
current--;
Log.d("PPK","notifay");
this.notify();
}
synchronized void reset(){
current=0;
alive=false;
this.notify();
}
synchronized void alive(){
alive=true;
}
@Override
public void run() {
Log.d("ImageSender", "starting thread");
try{
while(true){
Log.d("ImageSender", "looping");
while(alive && current<limit){
Log.d("ImageSender", "looping OK");
Log.d("PPK","looping with current" + current);
current++;
mCamera = getCameraInstance();
mCamera.takePicture(null, null, null, mPicture);
try {
Thread.sleep(1050); //50
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
synchronized (this) {
this.wait(1020);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}catch (Throwable t) {
// TODO: handle exception
Log.d("EX", "expection out of memory ?"+t );
t.printStackTrace();
clientState = new MyWebSocketListener();
}
}
}
private String TAG = "HTTPCamera";
private CameraSurfaceView cameraSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
clientState = new MyWebSocketListener();
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
}
private Camera mCamera;
private PicPreview mPreview;
private final static String TAG2 = "MainActivity";
private int mCount;
private FrameLayout preview;
//private SocketIO client;
@Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_main);
// Create an instance of Camera
mCamera = getCameraInstance();
Log.d(TAG2, "setting client done");
mPreview = new PicPreview(this, mCamera);
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(cameraSurfaceView);
Log.d(TAG, "setting preview done");
}
public void showToast(final String toast)
{
runOnUiThread(new Runnable() {
public void run()
{
Toast.makeText(MainActivity.this, toast, Toast.LENGTH_LONG).show();
}
});
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] byteData, Camera camera) {
Log.d(TAG2, "event: picture beginning");
Log.d(TAG2, "byte " + byteData);
Log.d(TAG2, "client: " + clientState);
clientState.send(byteData);
Log.d(TAG2, "event: picture sent");
}
};
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public Camera getCameraInstance(){
if (mCamera != null) {
return mCamera;
}
int cameraId=0;
int cameraCount = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
Camera.getCameraInfo( camIdx, cameraInfo );
if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
mCamera = Camera.open( camIdx );
Log.d(TAG2, "cameraId: " + cameraId);
return mCamera;
} catch (RuntimeException e) {
Log.e(TAG2, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return null;
}
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.