# Camera01 **Repository Path**: AndroidUI/Camera01 ## Basic Information - **Project Name**: Camera01 - **Description**: 调用是手机摄像头拍照并转换成file - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2016-09-19 - **Last Updated**: 2022-06-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 调用手机摄像头拍照,并显示+转换成file,保存在本地 # ## 逻辑: ## > activity_main.xml中有1个Button,1个imageView,点击button实现拍照,并显示在imageview中,而且保存在本地。 ## 步骤 ## 1. 调用手机摄像头拍照 2. 将数据转换成Bitmap 3. 将bitmap转换成file ## 效果图: ## ![](http://i.imgur.com/DHUjAHy.png) ![](http://i.imgur.com/bYH7jfC.png) ##知识点:## ###怎么将Bitmap存放在file中?### ``` public static void saveBitmapToFile(String path, Bitmap bm) { File file = new File(path); if (!file.exists()) { try { file.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } try { FileOutputStream fos = new FileOutputStream(file); bm.compress(CompressFormat.JPEG, 100, fos);//压缩格式,压缩比例 fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } ``` ###怎么将byte[]数据转换成file?### >通过FileoutputStream(File file) ``` File file = new File(path); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileOutputStream fos; try { fos = new FileOutputStream(file); fos.write(data, 0, data.length); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } ``` ###怎么调用系统摄像头?### >给intent添加action ``` Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1); ``` ###怎么获取手机摄像头拍照的图片,并转换成bitmap?### >在onActiivtyResult()方法中从intent中获取bundle,再从bundle获取bitmap ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap) bundle.get("data");//将数据转换成bitmap } } ``` >源码:https://git.oschina.net/beifang2008/Camera01 ## MainActivity(代码注释) ## ``` package com.android.tackcamera01; import java.io.IOException; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainActivity extends Activity { public String tag = "MainActivity"; public SurfaceView surfaceView; public Camera camera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); surfaceView = (SurfaceView) findViewById(R.id.surfaceview); SurfaceHolder holder = surfaceView.getHolder(); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.addCallback(new MyCallBack()); } class MyCallBack implements SurfaceHolder.Callback { @Override public void surfaceCreated(SurfaceHolder holder) { Log.d(tag, "###surfaceCreated"); camera = Camera.open(); try { camera.setPreviewDisplay(holder); camera.startPreview(); camera.takePicture(null, null, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // String path2 = "data/data/"+getPackageName()+"/"+System.currentTimeMillis()+ ".jpg"; // String path = Environment.getDataDirectory().getAbsolutePath()+ "/"+ System.currentTimeMillis()+ ".jpg"; // Log.d(tag, "###"+Environment.getDataDirectory().getAbsolutePath()); // FileUtil.saveFile(path, data); camera.startPreview(); } }); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.d(tag, "###surfaceChanged"); } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.d(tag, "###surfaceDestroyed"); if (camera != null) { camera.stopPreview(); camera.release(); camera = null; } } } } ``` ## Demo:http://download.csdn.net/detail/ss1168805219/9503230 ##