Bonjour à tous,
J'essais depuis quelques temps le développement Android. En ce moment, je tente de faire une application qui a besoin de la caméra, je voudrais le preview directement dans mon activity. J'ai utilisé des bout de codes de la doc notamment et comme résultat j'ai un preview qui reste blanc, la camera ne s'allume pas non plus (dans l'émulateur, j'utilise ma webcam intégrée)
Voici le code de mon activity qui utilise la caméra:
Le code qui créer le preview camera:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 package com.guillaimej.testapplication.app; import android.content.Intent; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageButton; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.widget.Toast; import android.view.SurfaceView; import android.view.SurfaceHolder; public class CameraInputActivity extends Activity{ private ImageButton returnButton = null; private ImageButton domygifButton = null; //private Camera camera = null; private CameraInputView cameraView; private SurfaceHolder previewHolder; private FrameLayout cameraLayout = null; // Check if the camera is free and return it private static Camera isCameraAvailable() { Camera cam = null; try { cam = Camera.open(0); } catch (Exception e) { } return cam; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera_input); returnButton = (ImageButton) findViewById(R.id.returnButton); returnButton.setOnTouchListener(new ReturnButtonListener()); domygifButton = (ImageButton) findViewById(R.id.domygif); cameraLayout = (FrameLayout) findViewById(R.id.camera_layout); Camera camera = isCameraAvailable(); // Create our Preview view and set it as the content of our activity. cameraView = new CameraInputView(this, camera); cameraLayout.addView(cameraView); } }
Et mon layout:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 package com.guillaimej.testapplication.app; import java.io.IOException; import android.content.Context; import android.hardware.Camera; import android.view.SurfaceHolder; import android.view.SurfaceView; public class CameraInputView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; public CameraInputView(Context context, Camera camera) { super(context); mCamera = camera; // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); // deprecated setting, but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, now tell the camera where to draw the preview. try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (IOException e) { System.out.println("Error setting camera preview: " + e.getMessage()); } } public void surfaceDestroyed(SurfaceHolder holder) { // empty. Take care of releasing the Camera preview in your activity. } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // If your preview can change or rotate, take care of those events here. // Make sure to stop the preview before resizing or reformatting it. if (mHolder.getSurface() == null){ // preview surface does not exist return; } // stop preview before making changes try { mCamera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or // reformatting changes here // start preview with new settings try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e){ System.out.println("Error starting camera preview: " + e.getMessage()); } } }
Dans mon manifest j'ai bien les 3 lignes suivantes:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.guillaimej.testapplication.app.CameraInputActivity"> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/camera_layout" android:layout_weight="0.2"> </FrameLayout> <ImageButton android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/domygif" android:layout_gravity="center_horizontal" android:background="@drawable/flask" android:clickable="true" android:onClick="snapIt" android:layout_weight="0.0"/> <ImageButton android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/returnButton" android:layout_gravity="center_horizontal" android:background="@drawable/return_button" android:layout_weight="0.0"/> </LinearLayout>
Quelqu'un aurait une idée de comment fixé ca ??? merci par avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 <uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />![]()
Partager