通过隐式意图来打开照相机
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv" /> </LinearLayout>
package com.itheima.camera; import java.io.File; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private File file ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } //打开的是前置摄像头 public void click(View view) { Intent intent = new Intent(); // 指定拍照的意图。 // MediaStore.ACTION_VIDEO_CAPTURE是录像的意图 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); // 指定保存文件的路径 startActivityForResult(intent, 100); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==100){ iv.setImageURI(Uri.fromFile(file)); } super.onActivityResult(requestCode, resultCode, data); } }
偷拍神奇
把预览框缩小成一个点拍照
通过Camera启动拍照资源
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" /> <FrameLayout android:id="@+id/camera_preview" android:layout_width="100dip" android:layout_height="100dip" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv" /> </LinearLayout>
package com.itheima.camera2; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.hardware.Camera.PictureCallback; import android.hardware.Camera.ShutterCallback; import android.os.Bundle; import android.os.Environment; import android.os.SystemClock; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private ImageView iv; private Camera mCamera; private CameraPreview mPreview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); // Create an instance of Camera mCamera = getCameraInstance(); // Create our Preview view and set it as the content of our activity. //获取预览界面 mPreview = new CameraPreview(this, mCamera); FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); //把预览界面加到帧布局 preview.addView(mPreview); } public void click(View view){ //拍照前先自动对焦一下 mCamera.autoFocus(new AutoFocusCallback() { //对焦完毕后拍照 @Override public void onAutoFocus(boolean success, Camera camera) { //第一参数为null 不关心快门 //数码相机拍出的默认照片是bitmap,没有经过压缩,体积过大,觉得体积过大了就可以null不去关心回调 mCamera.takePicture(null, null, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { File file = new File(Environment.getExternalStorageDirectory(),SystemClock.uptimeMillis()+".jpg"); FileOutputStream fos = new FileOutputStream(file); fos.write(data); fos.close(); Toast.makeText(getApplicationContext(), "成功", 0).show(); //拍完照片的时候,照片还是被占用的,如果还想再拍照,一定要先释放掉照相机的资源 //重新开启预览 mCamera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } }); } }); } /** 获取一个照相机实例 */ public static Camera getCameraInstance(){ //导入的是hardware包 Camera c = null; try { //打开照相机 open参数为1就是前置摄像头,空或者2就是后置摄像头 c = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } return c; // returns null if camera is unavailable } @Override protected void onDestroy() { //如果退出界面 要先停止照相机 mCamera.stopPreview(); //停止照相机之后就释放掉照相机资源 mCamera.release(); mCamera = null; super.onDestroy(); } }
package com.itheima.camera2; import java.io.IOException; import android.content.Context; import android.hardware.Camera; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; /** A basic Camera preview class */ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG = "CameraPreview"; private SurfaceHolder mHolder; private Camera mCamera; public CameraPreview(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) { Log.d(TAG, "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){ Log.d(TAG, "Error starting camera preview: " + e.getMessage()); } } }