この記事は最終更新日から1年以上経過しています。
Androidでカメラを扱うまとめです。
単純に撮った写真を保存するパターンと、パラメーターを指定して保存するパターン、
撮影した画像を圧縮して保存するパターンの3パターンを紹介します。
まずは、カメラを使用するために必要なパーミッションをAndroidManifest.xml
に記述します。
1 | < uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" /> |
2 | < uses-permission android:name = "android.permission.CAMERA" /> |
3 | < uses-permission android:name = "android.permission.FLASHLIGHT" /> |
5 | < uses-feature android:name = "android.hardware.camera" /> |
6 | < uses-feature android:name = "android.hardware.camera.autofocus" /> |
7 | < uses-feature android:name = "android.hardware.camera.flash" /> |
撮影処理をするSurfaceView
を継承したクラスを作成します。
まず、簡単に保存するパターン
4 | import android.util.Log; |
5 | import android.content.Context; |
7 | import android.view.SurfaceHolder; |
8 | import android.view.SurfaceView; |
9 | import android.view.MotionEvent; |
11 | import android.hardware.Camera; |
12 | import android.hardware.Camera.AutoFocusCallback; |
13 | import android.hardware.Camera.PictureCallback; |
14 | import android.hardware.Camera.ShutterCallback; |
16 | import android.graphics.*; |
17 | import android.provider.MediaStore; |
19 | public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback { |
20 | private Context mContext; |
21 | private Camera mCamera; |
22 | private boolean mFlag; |
23 | private static final String TAG = "MySurfaceView" ; |
25 | MySurfaceView(Context context) { |
28 | SurfaceHolder mHolder = getHolder(); |
29 | mHolder.addCallback( this ); |
30 | mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
33 | public void surfaceCreated(SurfaceHolder holder) { |
34 | mCamera = Camera.open(); |
36 | mCamera.setPreviewDisplay(holder); |
37 | } catch (IOException ex) { |
43 | public void surfaceDestroyed(SurfaceHolder holder) { |
44 | mCamera.stopPreview(); |
49 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
50 | setPictureFormat(format); |
52 | mCamera.startPreview(); |
56 | public boolean onTouchEvent(MotionEvent event) { |
64 | return super .onTouchEvent(event); |
67 | public void autoFocus() { |
70 | mCamera.autoFocus( new AutoFocusCallback() { |
72 | public void onAutoFocus( boolean success, final Camera camera) { |
74 | camera.takePicture(mShutterListener, |
81 | public void takePicture() { |
84 | if (mCamera != null ) { |
86 | mCamera.takePicture(mShutterListener, |
92 | ShutterCallback mShutterListener = new ShutterCallback() { |
94 | public void onShutter() { |
95 | Log.d(TAG, "onShutter" ); |
98 | PictureCallback rawListener = new PictureCallback() { |
100 | public void onPictureTaken( byte [] data, Camera camera) { |
101 | Log.d(TAG, "onPictureTaken: raw: data=" + data); |
106 | private PictureCallback jpegListener = new PictureCallback() { |
108 | public void onPictureTaken( byte [] data, Camera camera) { |
110 | Bitmap bmp = BitmapFactory.decodeByteArray(data, 0 , data.length, null ); |
112 | MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bmp, "sample" , null ); |
119 | } catch (InterruptedException ex) { |
123 | mCamera.startPreview(); |
129 | protected void setPreviewSize( int width, int height) { |
130 | Camera.Parameters params = mCamera.getParameters(); |
131 | List<Camera.Size> supported = params.getSupportedPreviewSizes(); |
132 | if (supported != null ) { |
133 | for (Camera.Size size : supported) { |
134 | if (size.width <= width && size.height <= height) { |
135 | params.setPreviewSize(size.width, size.height); |
136 | mCamera.setParameters(params); |
143 | protected void setPictureFormat( int format) { |
145 | Camera.Parameters params = mCamera.getParameters(); |
146 | List<Integer> supported = params.getSupportedPictureFormats(); |
147 | if (supported != null ) { |
148 | for ( int f : supported) { |
150 | params.setPreviewFormat(format); |
151 | mCamera.setParameters(params); |
156 | } catch (Exception e) { |
アクティビティを作成します。
1 | public class MyCameraActivity extends Activity { |
2 | private MySurfaceView mPreview; |
3 | private final int FC = ViewGroup.LayoutParams.FILL_PARENT; |
6 | protected void onCreate(Bundle savedInstanceState) { |
7 | super .onCreate(savedInstanceState); |
8 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
10 | mPreview = new MySurfaceView( this ); |
11 | setContentView(mPreview); |
14 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
18 | protected void onResume() { |
23 | protected void onPause() { |
25 | getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
30 | protected void onDestroy() { |
任意の場所や、パラメータを指定して保存する場合
4 | import android.util.Log; |
5 | import android.content.Context; |
6 | import android.content.ContentResolver; |
7 | import android.content.ContentValues; |
9 | import android.view.SurfaceHolder; |
10 | import android.view.SurfaceView; |
11 | import android.view.MotionEvent; |
13 | import android.hardware.Camera; |
14 | import android.hardware.Camera.AutoFocusCallback; |
15 | import android.hardware.Camera.PictureCallback; |
16 | import android.hardware.Camera.ShutterCallback; |
18 | import android.graphics.Bitmap; |
19 | import android.net.Uri; |
20 | import android.os.Environment; |
21 | import android.provider.MediaStore; |
22 | import android.provider.MediaStore.Images; |
24 | import android.text.format.DateFormat; |
26 | public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback { |
27 | private Context mContext; |
28 | private Camera mCamera; |
29 | private boolean mFlag; |
30 | private static final String TAG = "MySurfaceView" ; |
31 | private static final Uri IMAGE_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
32 | private static final String PATH = Environment.getExternalStorageDirectory().toString() + "/DCIM/Sample" ; |
34 | MySurfaceView(Context context) { |
37 | SurfaceHolder mHolder = getHolder(); |
38 | mHolder.addCallback( this ); |
39 | mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
42 | public void surfaceCreated(SurfaceHolder holder) { |
43 | mCamera = Camera.open(); |
45 | mCamera.setPreviewDisplay(holder); |
46 | } catch (IOException ex) { |
52 | public void surfaceDestroyed(SurfaceHolder holder) { |
53 | mCamera.stopPreview(); |
58 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
59 | setPictureFormat(format); |
61 | mCamera.startPreview(); |
65 | public boolean onTouchEvent(MotionEvent event) { |
73 | return super .onTouchEvent(event); |
76 | public void autoFocus() { |
79 | mCamera.autoFocus( new AutoFocusCallback() { |
81 | public void onAutoFocus( boolean success, final Camera camera) { |
83 | camera.takePicture(mShutterListener, |
90 | public void takePicture() { |
93 | if (mCamera != null ) { |
95 | mCamera.takePicture(mShutterListener, |
101 | ShutterCallback mShutterListener = new ShutterCallback() { |
103 | public void onShutter() { |
104 | Log.d(TAG, "onShutter" ); |
107 | PictureCallback rawListener = new PictureCallback() { |
109 | public void onPictureTaken( byte [] data, Camera camera) { |
110 | Log.d(TAG, "onPictureTaken: raw: data=" + data); |
115 | private PictureCallback jpegListener = new PictureCallback() { |
117 | public void onPictureTaken( byte [] data, Camera camera) { |
119 | addImageAsApplication(mContext.getContentResolver(), data); |
126 | } catch (InterruptedException ex) { |
130 | mCamera.startPreview(); |
136 | private static String createName( long dateTaken) { |
137 | return DateFormat.format( "yyyy-MM-dd_kk.mm.ss" , dateTaken).toString(); |
140 | public static Uri addImageAsApplication(ContentResolver cr, byte [] jpegData) { |
141 | long dateTaken = System.currentTimeMillis(); |
142 | String name = createName(dateTaken) + ".jpg" ; |
143 | return addImageAsApplication(cr, name, dateTaken, PATH, name, null , jpegData); |
146 | public static Uri addImageAsApplication(ContentResolver cr, String name, |
147 | long dateTaken, String directory, |
148 | String filename, Bitmap source, byte [] jpegData) { |
150 | OutputStream outputStream = null ; |
151 | String filePath = directory + "/" + filename; |
153 | File dir = new File(directory); |
157 | File file = new File(directory, filename); |
158 | if (file.createNewFile()) { |
159 | outputStream = new FileOutputStream(file); |
160 | outputStream.write(jpegData); |
163 | } catch (FileNotFoundException ex) { |
166 | } catch (IOException ex) { |
170 | if (outputStream != null ) { |
172 | outputStream.close(); |
173 | } catch (Throwable t) { |
178 | ContentValues values = new ContentValues( 7 ); |
179 | values.put(Images.Media.TITLE, name); |
180 | values.put(Images.Media.DISPLAY_NAME, filename); |
181 | values.put(Images.Media.DESCRIPTION, filename); |
182 | values.put(Images.Media.DATE_TAKEN, dateTaken); |
183 | values.put(Images.Media.MIME_TYPE, "image/jpeg" ); |
184 | values.put(Images.Media.DATA, filePath); |
185 | return cr.insert(IMAGE_URI, values); |
188 | protected void setPreviewSize( int width, int height) { |
189 | Camera.Parameters params = mCamera.getParameters(); |
190 | List<Camera.Size> supported = params.getSupportedPreviewSizes(); |
191 | if (supported != null ) { |
192 | for (Camera.Size size : supported) { |
193 | if (size.width <= width && size.height <= height) { |
194 | params.setPreviewSize(size.width, size.height); |
195 | mCamera.setParameters(params); |
202 | protected void setPictureFormat( int format) { |
204 | Camera.Parameters params = mCamera.getParameters(); |
205 | List<Integer> supported = params.getSupportedPictureFormats(); |
206 | if (supported != null ) { |
207 | for ( int f : supported) { |
209 | params.setPreviewFormat(format); |
210 | mCamera.setParameters(params); |
215 | } catch (Exception e) { |
圧縮して保存する場合
※写真を加工する際は一度Bitmapに変換する必要があります。
1 | import android.graphics.Bitmap.CompressFormat; |
4 | private PictureCallback jpegListener = new PictureCallback() { |
6 | public void onPictureTaken( byte [] data, Camera camera) { |
9 | Bitmap cameraMap = BitmapFactory.decodeByteArray(data, 0 , |
13 | addImageAsApplication(mContext.getContentResolver(), cameraMap); |
20 | } catch (InterruptedException ex) { |
24 | mCamera.startPreview(); |
30 | private static String createName( long dateTaken) { |
31 | return DateFormat.format( "yyyy-MM-dd_kk.mm.ss" , dateTaken).toString(); |
34 | public static Uri addImageAsApplication(ContentResolver cr, Bitmap bitmap) { |
35 | long dateTaken = System.currentTimeMillis(); |
36 | String name = createName(dateTaken) + ".jpg" ; |
37 | return addImageAsApplication(cr, name, dateTaken, PATH, name, bitmap, null ); |
40 | public static Uri addImageAsApplication(ContentResolver cr, String name, |
41 | long dateTaken, String directory, |
42 | String filename, Bitmap source, byte [] jpegData) { |
44 | OutputStream outputStream = null ; |
45 | String filePath = directory + "/" + filename; |
47 | File dir = new File(directory); |
51 | File file = new File(directory, filename); |
52 | if (file.createNewFile()) { |
53 | outputStream = new FileOutputStream(file); |
55 | source.compress(CompressFormat.JPEG, 75 , outputStream); |
57 | outputStream.write(jpegData); |
61 | } catch (FileNotFoundException ex) { |
64 | } catch (IOException ex) { |
68 | if (outputStream != null ) { |
71 | } catch (Throwable t) { |
76 | ContentValues values = new ContentValues( 7 ); |
77 | values.put(Images.Media.TITLE, name); |
78 | values.put(Images.Media.DISPLAY_NAME, filename); |
79 | values.put(Images.Media.DESCRIPTION, filename); |
80 | values.put(Images.Media.DATE_TAKEN, dateTaken); |
81 | values.put(Images.Media.MIME_TYPE, "image/jpeg" ); |
82 | values.put(Images.Media.DATA, filePath); |
83 | return cr.insert(IMAGE_URI, values); |
その他、カメラのパラメータ色々
1 | protected void setPictureFormat( int format) { |
3 | Camera.Parameters params = camera.getParameters(); |
4 | List<Integer> supported = params.getSupportedPictureFormats(); |
5 | if (supported != null ) { |
6 | for ( int f : supported) { |
8 | params.setPreviewFormat(format); |
9 | camera.setParameters(params); |
14 | } catch (Exception e) { |
19 | protected void setPreviewSize( int width, int height) { |
20 | Camera.Parameters params = camera.getParameters(); |
21 | List<Camera.Size> supported = params.getSupportedPreviewSizes(); |
22 | if (supported != null ) { |
23 | for (Camera.Size size : supported) { |
24 | if (size.width <= width && size.height <= height) { |
25 | params.setPreviewSize(size.width, size.height); |
26 | camera.setParameters(params); |
33 | protected void setAntibanding(String antibanding) { |
34 | Camera.Parameters params = camera.getParameters(); |
35 | List<String> supported = params.getSupportedAntibanding(); |
36 | if (supported != null ) { |
37 | for (String ab : supported) { |
38 | if (ab.equals(antibanding)) { |
39 | params.setAntibanding(antibanding); |
40 | camera.setParameters(params); |
47 | protected void setColorEffect(String effect) { |
48 | Camera.Parameters params = camera.getParameters(); |
49 | List<String> supported = params.getSupportedColorEffects(); |
50 | if (supported != null ) { |
51 | for (String e : supported) { |
52 | if (e.equals(effect)) { |
53 | params.setColorEffect(effect); |
54 | camera.setParameters(params); |
61 | protected void setFlashMode(String flash_mode) { |
62 | Camera.Parameters params = camera.getParameters(); |
63 | List<String> supported = params.getSupportedFlashModes(); |
64 | if (supported != null ) { |
65 | for (String fm : supported) { |
66 | if (fm.equals(flash_mode)) { |
67 | params.setFlashMode(flash_mode); |
68 | camera.setParameters(params); |
75 | protected void setFocusMode(String focus_mode) { |
76 | Camera.Parameters params = camera.getParameters(); |
77 | List<String> supported = params.getSupportedFocusModes(); |
78 | if (supported != null ) { |
79 | for (String fm : supported) { |
80 | if (fm.equals(focus_mode)) { |
81 | params.setFocusMode(focus_mode); |
82 | camera.setParameters(params); |
89 | protected void setSceneMode(String scene_mode) { |
90 | Camera.Parameters params = camera.getParameters(); |
91 | List<String> supported = params.getSupportedSceneModes(); |
92 | if (supported != null ) { |
93 | for (String sm : supported) { |
94 | if (sm.equals(scene_mode)) { |
95 | params.setSceneMode(scene_mode); |
96 | camera.setParameters(params); |
103 | protected void setWhiteBalance(String white_balance) { |
104 | Camera.Parameters params = camera.getParameters(); |
105 | List<String> supported = params.getSupportedWhiteBalance(); |
106 | if (supported != null ) { |
107 | for (String wb : supported) { |
108 | if (wb.equals(white_balance)) { |
109 | params.setWhiteBalance(white_balance); |
110 | camera.setParameters(params); |
117 | protected void setColorEffects(String color_effects) { |
118 | Camera.Parameters params = mCamera.getParameters(); |
119 | List<String> supported = params.getSupportedColorEffects(); |
120 | if (supported != null ) { |
121 | for (String ce : supported) { |
122 | if (ce.equals(color_effects)) { |
123 | params.setColorEffect(color_effects); |
124 | mCamera.setParameters(params); |