Android拍照
1.配置許可權
<uses-feature android:name="android.hardware.camera" /><!-- 照相機硬體裝置特性 -->
<uses-permission android:name="android.permission.CAMERA" /><!-- 照相機許可權 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 寫到外儲存裝置許可權(寫到SD卡) -->
2.在佈局檔案中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.image.MainActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical">
<!-- 建立出來的預覽類新增到FrameLayout裡,這裡是因為祕密拍攝,將寬高設定成1dp -->
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="1dp"
android:layout_height="1dp"/>
<Button
android:onClick="takePicture"
android:text="拍照"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3.建立照相機畫面預覽類
/**
* 照相機畫面預覽類
* @author Administrator
*
*/
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());
}
}
}
4.MainActivity
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.獲取照相機例項
mCamera = getCameraInstance();
//2.獲取照相機預覽類
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);//3.將照相機的預覽介面加入到指定的FrameLayout裡
}
//當點選按鈕的時候進行拍照
public void takePicture(View view){
mCamera.autoFocus(null);//自動對焦,引數是個自動對焦完畢後的回撥函式(一般是在這個回撥函式裡拍照)
/**
* 1.引數一:按下快門的時候的回撥函式(一般是呼叫快門聲音)
* 2.引數二:當相機獲取原始照片時觸發該監聽器
* 3.引數三:當相機獲取JPG照片時觸發該監聽器
*/
mCamera.takePicture(null, null, mPicture);
}
/**
* 獲取一個照相機例項
* @return
*/
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(0); // 獲取後置攝像頭 open(1)前置 open(0)和open()都是後置
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private PictureCallback mPicture = new PictureCallback() {
private String TAG = "MainActivity";
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = new File(Environment.getExternalStorageDirectory(),"123.jpg");
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (Exception e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
protected void onDestroy() {
super.onDestroy();
if(mCamera!=null){
mCamera.stopPreview();//停止預覽
mCamera.release();//釋放資源
mCamera = null;
}
};
}
相關文章
- android 拍照Android
- Android Media(拍照)Android
- Android Camera——拍照Android
- Android呼叫攝像頭拍照Android
- Android自定義拍照實現Android
- Android 拍照新增時間水印Android
- Android 呼叫攝像頭拍照Android
- Android提供的攝像頭拍照Android
- Android本地圖片上傳(拍照+相簿)Android地圖
- Android 拍照、選擇圖片並裁剪Android
- android 實現拍照的2種方法Android
- 照片系列之android呼叫攝像頭拍照Android
- Android開發之呼叫攝像頭拍照Android
- Android呼叫系統相簿和相機拍照Android
- Android Studio 呼叫Camera實現拍照功能Android
- Android呼叫攝像頭拍照並顯示照片Android
- Android7.0拍照以及使用uCrop裁剪Android
- Android 呼叫攝像頭功能【拍照與視訊】Android
- Android 圓形頭像 相簿和拍照裁剪選取Android
- 【Android】【opencv】實現攝像頭拍照和錄影AndroidOpenCV
- Android拍照與相機適配問題彙總Android
- Android 呼叫系統相機拍照 . 選取本地相簿Android
- Android 從本地選取圖片或者拍照填充ImageViewAndroidView
- Android拍照,相簿選擇圖片以及Android6.0許可權管理Android
- iOS自定義拍照框拍照&裁剪(一)iOS
- Android | 教你如何開發一個拍照翻譯小程式Android
- 在Android中呼叫攝像頭拍照並顯示出來Android
- 短視訊程式開發,Android:呼叫系統拍照和相簿Android
- android實現拍照、相簿選圖、裁剪功能,相容7.0以及小米Android
- Android多媒體應用開發-控制攝像頭拍照Android
- Android 拍照及相簿選取圖片功能,已適配Android6.0、7.0、8.0Android
- 直播平臺軟體開發,Android 10 拍照和相簿選擇Android
- Android 用MultiImageSelector實現上傳頭像的拍照跟相簿Android
- GPUImage之拍照篇GPUUI
- Android:呼叫系統相機實現拍照+裁切(相容7.0以上系統)Android
- Android WebView 實現檔案選擇、拍照、錄製視訊、錄音AndroidWebView
- Android開發獲取相機拍照的原圖(並非縮圖)Android
- Android學習之呼叫系統相機拍照、截圖並儲存Android