Prise de photo automatique et preview Camera
Nalianne
Messages postés
8
Date d'inscription
Statut
Membre
Dernière intervention
-
Nalianne Messages postés 8 Date d'inscription Statut Membre Dernière intervention -
Nalianne Messages postés 8 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
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.
Soit une image blanche s'affiche (dans le cas ou j'enlève tout affichage de preview) soit (lorsque je set la preview) l'application plante
Logcat:
MainActivity:
Activity_main.xml
Manifest:
Est ce que quelqu'un pourrait m'aider sur ce problème ?
Merci d'avance !
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.
Soit une image blanche s'affiche (dans le cas ou j'enlève tout affichage de preview) soit (lorsque je set la preview) l'application plante
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 ComponentInfo{com.example.photopic/com.example.photopic.MainActivity}: java.lang.NullPointerException 03-29 10:51:16.575: E/AndroidRuntime(660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.app.ActivityThread.access$600(ActivityThread.java:127) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.os.Handler.dispatchMessage(Handler.java:99) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.os.Looper.loop(Looper.java:137) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.app.ActivityThread.main(ActivityThread.java:4511) 03-29 10:51:16.575: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method) 03-29 10:51:16.575: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:511) 03-29 10:51:16.575: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 03-29 10:51:16.575: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 03-29 10:51:16.575: E/AndroidRuntime(660): at dalvik.system.NativeStart.main(Native Method) 03-29 10:51:16.575: E/AndroidRuntime(660): Caused by: java.lang.NullPointerException 03-29 10:51:16.575: E/AndroidRuntime(660): at com.example.photopic.MainActivity.onCreate(MainActivity.java:243) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.app.Activity.performCreate(Activity.java:4470) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 03-29 10:51:16.575: E/AndroidRuntime(660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 03-29 10:51:16.575: E/AndroidRuntime(660): ... 11 more
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(); try { mCamera.takePicture(null, null, null, mPicture); } catch(Throwable e) { Log.d("ERROR", "Throwable "+e.getMessage()); Log.d("ERROR", "Throwable2 "+e.toString()); } 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; } } @Override protected void onStop() { super.onStop(); // Always call the superclass method first } @Override protected void onStart() { super.onStart(); // Always call the superclass method first } @Override protected void onRestart() { super.onRestart(); // Always call the superclass method first // Activity being restarted from stopped state } 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; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <FrameLayout android:id="@+id/camera_preview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> </LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.photopic" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.photopic.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Est ce que quelqu'un pourrait m'aider sur ce problème ?
Merci d'avance !
A voir également:
- Prise de photo automatique et preview Camera
- Google photo - Télécharger - Albums photo
- Réponse automatique thunderbird - Guide
- Photo aérienne de ma maison - Guide
- Photo filtre 7 gratuit - Télécharger - Retouche d'image
- Partage photo - Guide