1 |
package com.caddi.android.caddipictureupload; |
2 |
|
3 |
import android.app.ActionBar; |
4 |
import android.app.Activity; |
5 |
import android.content.Intent; |
6 |
import android.graphics.Bitmap; |
7 |
import android.graphics.BitmapFactory; |
8 |
import android.hardware.Camera; |
9 |
import android.support.v7.app.ActionBarActivity; |
10 |
import android.os.Bundle; |
11 |
import android.util.Log; |
12 |
import android.view.Menu; |
13 |
import android.view.MenuItem; |
14 |
import android.view.Surface; |
15 |
import android.view.SurfaceHolder; |
16 |
import android.view.SurfaceView; |
17 |
import android.view.View; |
18 |
import android.view.ViewGroup; |
19 |
|
20 |
import com.caddi.android.caddipictureupload.util.PictureUtil; |
21 |
|
22 |
import java.io.IOException; |
23 |
import java.util.List; |
24 |
|
25 |
|
26 |
public class CameraActivity extends Activity { |
27 |
|
28 |
public static final String IMGDATA = "IMGDATA"; |
29 |
public static final String IMGORIENTATION = "ORIENTATION"; |
30 |
|
31 |
|
32 |
Camera cam; |
33 |
SurfaceView surfaceView; |
34 |
|
35 |
int orientation = -1; |
36 |
|
37 |
@Override |
38 |
protected void onCreate(Bundle savedInstanceState) { |
39 |
super.onCreate(savedInstanceState); |
40 |
setContentView(R.layout.activity_camera); |
41 |
|
42 |
surfaceView = (SurfaceView) findViewById(R.id.camSurfaceView); |
43 |
|
44 |
ViewGroup.LayoutParams params = surfaceView.getLayoutParams(); |
45 |
Log.i("CAM", "W:" + params.width); |
46 |
Log.i("CAM", "H:" + params.height); |
47 |
|
48 |
|
49 |
|
50 |
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { |
51 |
@Override |
52 |
public void surfaceCreated(SurfaceHolder holder) { |
53 |
Log.i("CAM", "Surface created"); |
54 |
try { |
55 |
cam.setPreviewDisplay(holder); |
56 |
cam.startPreview(); |
57 |
} catch (IOException e) { |
58 |
Log.d("CAM", "Error opening cam", e); |
59 |
} |
60 |
} |
61 |
|
62 |
@Override |
63 |
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
64 |
|
65 |
} |
66 |
|
67 |
@Override |
68 |
public void surfaceDestroyed(SurfaceHolder holder) { |
69 |
|
70 |
} |
71 |
}); |
72 |
|
73 |
surfaceView.setOnClickListener(new View.OnClickListener() { |
74 |
@Override |
75 |
public void onClick(View v) { |
76 |
cam.takePicture(null/*Shuttercallback*/, |
77 |
null/*RawCallback*/, |
78 |
new Camera.PictureCallback() {//jpeg |
79 |
@Override |
80 |
public void onPictureTaken(byte[] data, Camera camera) { |
81 |
|
82 |
Log.d("CaddiCam", "Size:" + data.length); |
83 |
|
84 |
camera.release(); |
85 |
|
86 |
|
87 |
Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length); |
88 |
if (orientation != 0) { |
89 |
image = PictureUtil.rotateImage(image, orientation); |
90 |
} |
91 |
image = PictureUtil.clipCenter(image); |
92 |
|
93 |
PictureSingleton.getInstance().setPicture(image);//Gem bitmap i app singleton |
94 |
|
95 |
|
96 |
Intent resultIntent = new Intent(); |
97 |
Bundle extras = new Bundle(); |
98 |
extras.putInt(IMGORIENTATION, orientation); |
99 |
|
100 |
resultIntent.putExtras(extras); |
101 |
|
102 |
CameraActivity.this.setResult(Activity.RESULT_OK, resultIntent); |
103 |
CameraActivity.this.finish(); |
104 |
} |
105 |
} |
106 |
|
107 |
); |
108 |
} |
109 |
}); |
110 |
|
111 |
int camId = getBackfacingCamera(); |
112 |
|
113 |
|
114 |
|
115 |
|
116 |
cam = Camera.open(camId); |
117 |
orientation = setCameraDisplayOrientation(this, camId, cam);// |
118 |
|
119 |
Camera.Parameters camParams = cam.getParameters(); |
120 |
camParams.setJpegQuality(90); |
121 |
|
122 |
Log.i("CADDIcam", "Flash: " + camParams.getFlashMode()); |
123 |
Log.i("CADDIcam", "Focus: " + camParams.getFocusMode() ); |
124 |
Log.i("CADDIcam", "Scene: " + camParams.getSceneMode() ); |
125 |
Log.i("CADDIcam", "White: " + camParams.getWhiteBalance() ); |
126 |
|
127 |
Camera.Size s = camParams.getPictureSize(); |
128 |
Log.i("CADDIcam", "Size: " + s.width + ", " + s.height); |
129 |
|
130 |
List<Camera.Size> sizes = camParams.getSupportedPictureSizes(); |
131 |
Camera.Size bestSize = null; |
132 |
for(Camera.Size cur : sizes) |
133 |
{ |
134 |
if (cur.width > 1600 || cur.height > 1600) |
135 |
continue; |
136 |
|
137 |
if (bestSize == null) { |
138 |
bestSize = cur; |
139 |
continue; |
140 |
} |
141 |
|
142 |
if(cur.width > bestSize.width) { |
143 |
bestSize = cur; |
144 |
} |
145 |
} |
146 |
Log.i("CADDIcam", "BestSize: " +bestSize.width + ", " + bestSize.height); |
147 |
camParams.setPictureSize(bestSize.width, bestSize.height); |
148 |
|
149 |
cam.setParameters(camParams); |
150 |
|
151 |
|
152 |
} |
153 |
|
154 |
@Override |
155 |
protected void onPause() { |
156 |
super.onPause();; |
157 |
cam.release(); |
158 |
} |
159 |
|
160 |
/* |
161 |
* Copied from http://developer.android.com/reference/android/hardware/Camera.html |
162 |
*/ |
163 |
public static int setCameraDisplayOrientation(Activity activity, |
164 |
int cameraId, |
165 |
android.hardware.Camera camera) { |
166 |
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); |
167 |
|
168 |
android.hardware.Camera.getCameraInfo(cameraId, info); |
169 |
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); |
170 |
int degrees = 0; |
171 |
|
172 |
switch (rotation) { |
173 |
case Surface.ROTATION_0: degrees = 0; break; |
174 |
case Surface.ROTATION_90: degrees = 90; break; |
175 |
case Surface.ROTATION_180: degrees = 180; break; |
176 |
case Surface.ROTATION_270: degrees = 270; break; |
177 |
} |
178 |
|
179 |
int result; |
180 |
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { |
181 |
result = (info.orientation + degrees) % 360; |
182 |
result = (360 - result) % 360; // compensate the mirror |
183 |
} else { // back-facing |
184 |
result = (info.orientation - degrees + 360) % 360; |
185 |
} |
186 |
camera.setDisplayOrientation(result); |
187 |
|
188 |
return result; |
189 |
} |
190 |
|
191 |
|
192 |
|
193 |
public static int getBackfacingCamera() { |
194 |
int numberOfCams = Camera.getNumberOfCameras(); |
195 |
|
196 |
for (int i=0; i<numberOfCams; i++) { |
197 |
Camera.CameraInfo info = new Camera.CameraInfo(); |
198 |
Camera.getCameraInfo(i, info); |
199 |
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { |
200 |
return i; |
201 |
} |
202 |
} |
203 |
return -1; |
204 |
} |
205 |
|
206 |
|
207 |
|
208 |
} |