/[projects]/android/BarcodeSample/app/src/main/java/dk/thoerup/android/barcodesample/CamActivity.java
ViewVC logotype

Contents of /android/BarcodeSample/app/src/main/java/dk/thoerup/android/barcodesample/CamActivity.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2559 - (show annotations) (download)
Fri May 22 13:26:16 2015 UTC (8 years, 11 months ago) by torben
File size: 5119 byte(s)
Add very basic camera functionality
1 package dk.thoerup.android.barcodesample;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.hardware.Camera;
6 import android.support.v7.app.ActionBarActivity;
7 import android.os.Bundle;
8 import android.util.Log;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.Surface;
12 import android.view.SurfaceHolder;
13 import android.view.SurfaceView;
14 import android.view.View;
15
16 import java.io.IOException;
17
18
19 public class CamActivity extends ActionBarActivity {
20
21
22 Camera cam;
23 SurfaceView surfaceView;
24
25 @Override
26 protected void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.activity_cam);
29
30
31 surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
32 surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
33 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
34 }
35
36 public void surfaceCreated(SurfaceHolder holder) {
37 Log.i("CAM", "surface created");
38 try {
39 cam.setPreviewDisplay(holder);
40 cam.startPreview();
41 } catch (IOException e) {
42 Log.d("CAM", "Error opening cam", e);
43 }
44
45
46 }
47
48 public void surfaceDestroyed(SurfaceHolder holder) {
49 }
50 });
51
52 surfaceView.setOnClickListener(new View.OnClickListener() {
53 @Override
54 public void onClick(View v) {
55 cam.takePicture(
56 new Camera.ShutterCallback() {
57 @Override
58 public void onShutter() {
59 }
60 },
61 new Camera.PictureCallback() {//raw
62 @Override
63 public void onPictureTaken(byte[] data, Camera camera) {
64
65 }
66 },
67 new Camera.PictureCallback() {//postview
68 @Override
69 public void onPictureTaken(byte[] data, Camera camera) {
70
71 }
72 },
73 new Camera.PictureCallback() { //JPEG
74 @Override
75 public void onPictureTaken(byte[] data, Camera camera) {
76 cam.release();
77
78 Intent resultIntent = new Intent();
79 resultIntent.putExtra("DATA", data);
80 CamActivity.this.setResult(Activity.RESULT_OK, resultIntent);
81
82 CamActivity.this.finish();
83 }
84 });
85
86 }
87
88 });
89
90 cam = Camera.open();
91 setCameraDisplayOrientation(this,0,cam);
92 }
93
94 /*
95 * lånt fra
96 * http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)
97 */
98 public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
99 android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
100 android.hardware.Camera.getCameraInfo(cameraId, info);
101 int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
102
103 int degrees = 0;
104 switch (rotation) {
105 case Surface.ROTATION_0: degrees = 0; break;
106 case Surface.ROTATION_90: degrees = 90; break;
107 case Surface.ROTATION_180: degrees = 180; break;
108 case Surface.ROTATION_270: degrees = 270; break;
109 }
110
111 int result;
112 if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
113 result = (info.orientation + degrees) % 360;
114 result = (360 - result) % 360; // compensate the mirror
115 } else { // back-facing
116 result = (info.orientation - degrees + 360) % 360;
117 }
118 camera.setDisplayOrientation(result);
119 }
120
121
122 @Override
123 protected void onPause () {
124 super.onPause();
125 cam.release();
126 }
127
128 @Override
129 public boolean onCreateOptionsMenu(Menu menu) {
130 // Inflate the menu; this adds items to the action bar if it is present.
131 getMenuInflater().inflate(R.menu.menu_cam, menu);
132 return true;
133 }
134
135 @Override
136 public boolean onOptionsItemSelected(MenuItem item) {
137 // Handle action bar item clicks here. The action bar will
138 // automatically handle clicks on the Home/Up button, so long
139 // as you specify a parent activity in AndroidManifest.xml.
140 int id = item.getItemId();
141
142 //noinspection SimplifiableIfStatement
143 if (id == R.id.action_settings) {
144 return true;
145 }
146
147 return super.onOptionsItemSelected(item);
148 }
149 }

  ViewVC Help
Powered by ViewVC 1.1.20