android自定义拍照

调用系统相机,然后在自己的surfaceview上预览,拍照,不废话,直接上源码

  1 package com.example.customecamera;
  2 
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 
  8 import android.app.Activity;
  9 import android.content.Intent;
 10 import android.graphics.Bitmap;
 11 import android.graphics.BitmapFactory;
 12 import android.graphics.ImageFormat;
 13 import android.hardware.Camera;
 14 import android.hardware.Camera.AutoFocusCallback;
 15 import android.hardware.Camera.Parameters;
 16 import android.hardware.Camera.PictureCallback;
 17 import android.os.Bundle;
 18 import android.os.Environment;
 19 import android.view.SurfaceHolder;
 20 import android.view.SurfaceView;
 21 import android.view.View;
 22 
 23 public class MainActivity extends Activity implements SurfaceHolder.Callback {
 24 
 25     SurfaceView mSurfaceview;
 26     Camera mCamera;
 27     SurfaceHolder mHolder;
 28 
 29     @Override
 30     protected void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.activity_main);
 33         mSurfaceview = (SurfaceView) findViewById(R.id.surfaceview);
 34         mHolder = mSurfaceview.getHolder();
 35         mHolder.addCallback(this);
 36     }
 37     
 38     PictureCallback callback =    new PictureCallback() {
 39         
 40         @Override
 41         public void onPictureTaken(byte[] data, Camera camera) {
 42             if(data != null) {
 43                 try {
 44                     File file = new File(Environment.getExternalStorageDirectory() + "/" + "temp.jpg");
 45                     FileOutputStream fos = new FileOutputStream(file);
 46                     fos.write(data, 0, data.length);
 47                     Intent  intent = new Intent(MainActivity.this , OtherActivity.class);
 48                     intent.putExtra("pic_path", file.getAbsolutePath());
 49                     startActivity(intent);
 50                 } catch (FileNotFoundException e) {
 51                     // TODO Auto-generated catch block
 52                     e.printStackTrace();
 53                 } catch (IOException e) {
 54                     // TODO Auto-generated catch block
 55                     e.printStackTrace();
 56                 }
 57                 
 58             }
 59         }
 60         
 61     };
 62 
 63     @SuppressWarnings("deprecation")
 64     public void caputer(View v) {
 65         Parameters parameters = mCamera.getParameters();
 66         parameters.setPictureFormat(ImageFormat.JPEG);
 67         parameters.setPictureSize(400, 600);
 68         parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
 69         mCamera.autoFocus(new AutoFocusCallback() {
 70             
 71             @Override
 72             public void onAutoFocus(boolean success, Camera camera) {
 73                 mCamera.takePicture(null, null, callback);
 74             }
 75         });
 76     
 77     }
 78 
 79     public Camera getCamera() {
 80         Camera camera = Camera.open();
 81         return camera;
 82     }
 83 
 84     public void relaseCamer() {
 85         if (mCamera != null) {
 86             mCamera.setPreviewCallback(null);
 87             mCamera.stopPreview();
 88             mCamera.release();
 89             mCamera = null;
 90         }
 91     }
 92 
 93     public void startPreview(Camera camera, SurfaceHolder holder) {
 94         try {
 95             camera.setPreviewDisplay(holder);
 96             camera.setDisplayOrientation(90);
 97             camera.startPreview();
 98         } catch (IOException e) {
 99             // TODO Auto-generated catch block
100             e.printStackTrace();
101         }
102     }
103 
104     @Override
105     protected void onPause() {
106         super.onPause();
107         relaseCamer();
108     }
109 
110     @Override
111     protected void onResume() {
112         super.onResume();
113         if (mCamera == null) {
114             mCamera = getCamera();
115             if(mHolder != null) {
116                 startPreview(mCamera, mHolder);
117             }
118         }
119     }
120 
121     @Override
122     public void surfaceCreated(SurfaceHolder holder) {
123         startPreview(mCamera, mHolder);
124     }
125 
126     @Override
127     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
128         mCamera.stopPreview();
129         startPreview(mCamera, mHolder);
130     }
131 
132     @Override
133     public void surfaceDestroyed(SurfaceHolder holder) {
134         relaseCamer();
135     }
136     
137 }
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="${relativePackage}.${activityClass}" >
 6 
 7    
 8     <SurfaceView 
 9         android:id="@+id/surfaceview"
10         android:layout_width="match_parent"
11         android:layout_height="400dp" />
12      <Button
13         android:id="@+id/button1"
14         android:layout_below="@id/surfaceview"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:layout_centerHorizontal="true"
18         android:onClick="caputer"
19         android:text="拍照" />
20 
21 </RelativeLayout>
 1 package com.example.customecamera;
 2 
 3 import android.app.Activity;
 4 import android.graphics.Bitmap;
 5 import android.graphics.BitmapFactory;
 6 import android.graphics.Matrix;
 7 import android.os.Bundle;
 8 import android.text.TextUtils;
 9 import android.widget.ImageView;
10 
11 public class OtherActivity extends Activity {
12     ImageView iv;
13     
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.other_activity);
18         iv = (ImageView) findViewById(R.id.imageView1);
19         String path = getIntent().getStringExtra("pic_path");
20         if(!TextUtils.isEmpty(path)) {
21             Bitmap bitmap = BitmapFactory.decodeFile(path);
22             Matrix matrix = new Matrix();
23             matrix.postRotate(90);
24             bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
25             if(bitmap != null) {
26                 iv.setImageBitmap(bitmap);
27             }
28         }
29     }
30 
31 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <ImageView
 8         android:id="@+id/imageView1"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_gravity="center"       
12 />
13 
14 </LinearLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.customecamera"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="21" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name=".MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <activity android:name=".OtherActivity" > 
26         </activity>
27     </application>
28 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
29 <uses-permission android:name="android.permission.CAMERA"/>
30 </manifest>

 

转载于:https://www.cnblogs.com/androidsihai/p/6033435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值