Ⅰ android camera如何判斷當前使用的攝像頭是前置還是後置
現在 android 平台的智能手機一般都標配有兩顆攝像頭。在 Camera 中都存在攝像頭切換的功能。
並且有一些功能前後置攝像頭上會有所不同。譬如人臉檢測,人臉識別,自動對焦,閃光燈等功能,
如果前置攝像頭的像素太低,不支持該功能的話,就需要在前置攝像頭上關掉該 feature.
那麼是如何判斷並切換前後置攝像頭的呢?
我們先來看下 CameraInfo 這個類,
[java] view plain
/**
* Information about a camera
*/
public static class CameraInfo {
/**
* The facing of the camera is opposite to that of the screen.
*/
public static final int CAMERA_FACING_BACK = 0;
/**
* The facing of the camera is the same as that of the screen.
*/
public static final int CAMERA_FACING_FRONT = 1;
/**
* The direction that the camera faces. It should be
* CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
*/
public int facing;
/**
* <p>The orientation of the camera image. The value is the angle that the
* camera image needs to be rotated clockwise so it shows correctly on
* the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
*
* <p>For example, suppose a device has a naturally tall screen. The
* back-facing camera sensor is mounted in landscape. You are looking at
* the screen. If the top side of the camera sensor is aligned with the
* right edge of the screen in natural orientation, the value should be
* 90. If the top side of a front-facing camera sensor is aligned with
* the right of the screen, the value should be 270.</p>
*
* @see #setDisplayOrientation(int)
* @see Parameters#setRotation(int)
* @see Parameters#setPreviewSize(int, int)
* @see Parameters#setPictureSize(int, int)
* @see Parameters#setJpegThumbnailSize(int, int)
*/
public int orientation;
};
見名知義,它就是一個 Camera 信息類。它是通過與屏幕的方向是否一致來定義前後置攝像頭的。
與屏幕方向相反即為 BACK_FACING_CAMERA
與屏幕方向一致即為 FRONT_FACING_CAMERA
那麼在代碼中我們是如何獲取當前使用的 CamerInfo 呢
[java] view plain
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
當然,使用該代碼的前提是要 import android.hardware.Camera.CameraInfo;
判斷使用是前置還是後置攝像頭,可以通過if (info.facing == CameraInfo.CAMERA_FACING_FRONT) 來判斷。
當Camera 的實例已經創建了的情況下,則需要通過如下方式來判斷。
[java] view plain
CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
//stopFaceDetection();
}
也可以通過 if(mCameraId == CameraInfo.CAMERA_FACING_FRONT) 來判斷。
其中 mCameraId 是當前使用的 CameraId, 一般前置為1, 後置為 0。
Ⅱ 檢查Android是否具有攝像頭
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Build;
public class CameraUtils {
/**
* 檢測是否有前置攝像頭
* @return
*/
public static boolean hasFrontFacingCamera() {
final int CAMERA_FACING_BACK = 1;
return checkCameraFacing(CAMERA_FACING_BACK);
}
/**
* 檢測是否有後置攝像頭
* @return
*/
public static boolean hasBackFacingCamera() {
final int CAMERA_FACING_BACK = 0;
return checkCameraFacing(CAMERA_FACING_BACK);
}
private static boolean checkCameraFacing(final int facing) {
if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {
return false;
}
final int cameraCount = Camera.getNumberOfCameras();
CameraInfo info = new CameraInfo();
for (int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, info);
if (facing == info.facing) {
return true;
}
}
return false;
}
public static int getSdkVersion() {
return android.os.Build.VERSION.SDK_INT;
}
}
Ⅲ 如何自定義開啟android攝像頭
開啟攝像頭的過程如下:
Android提供了Camera來控制拍照,步驟如下:
(1)調用Camera的open()方法打開相機。
(2)調用Camera的getParameters()獲取拍照參數,該方法返回一個Cmera.Parameters對象。
(3)調用Camera.Parameters對象對照相的參數進行設置。
(4)調用Camera的setParameters(),並將Camera.Parameters對象作為參數傳入,這樣就可以對拍照進行參數控制,Android2.3.3以後不用設置。
(5)調用Camerade的startPreview()的方法開始預覽取景,在之前需要調用Camera的setPreviewDisplay(SurfaceHolder holder)設置使用哪個SurfaceView來顯示取得的圖片。
(6)調用Camera的takePicture()方法進行拍照。
(7)程序結束時,要調用Camera的stopPreview()方法停止預覽,並且通過Camera.release()來釋放資源。
需要賦予Camera的許可權:
1
2
3
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.CAMERA"/>
下面上代碼:
Ⅳ Android上可以做攝像頭實時的處理嗎
由於處理視頻幀肯定耗時,所以不能絕對的實時
在獲取預覽幀的回調函數裡面可以對攝像頭返回的數據進行處理
Ⅳ Android開發(攝像頭的調用)(大神請進……)
打開攝像頭,要 start surfaceiew,當activity執行到onStop()時,要判斷,然後關閉surfaceiew。這樣應該就不會導致程序force close了。或者樓主您直接把force close的log信息打出來,有log才能准確的分析問題。
Ⅵ android Camera 如何判斷當前使用的攝像頭是前置還是後置
在你打開camera的時候,你會傳入一個cameraId:
public static Camera open (int cameraId)
當你設置了cameraId,打開camera之後,可以通過下面的代碼獲取camera信息:
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
//前置攝像頭
} else { // 後置攝像頭
}
Ⅶ android中使用攝像頭可以掃描中文嗎
安卓上名片掃描軟體很多,比如「名片全能王」,基本全部都是支持2.2的系統的。但是手機的攝像頭要有微距拍攝功能,不然會因為模糊而無法識別字。
Ⅷ 檢查Android是否具有攝像頭且為可用
public static boolean checkCameraEnable() {
boolean result;
Camera camera = null;
try {
camera = Camera.open();
if (camera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
boolean connected = false;
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(camIdx) + ")");
try {
camera = Camera.open(camIdx);
connected = true;
} catch (RuntimeException e) {
Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage());
}
if (connected) {
break;
}
}
}
List<Camera.Size> supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes();
result = supportedPreviewSizes != null;
/* Finally we are ready to start the preview */
Log.d(TAG, "startPreview");
camera.startPreview();
} catch (Exception e) {
Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage());
result = false;
} finally {
if (camera != null) {
camera.release();
}
}
return result;
}
Ⅸ 如何在android中後台開啟攝像頭默默拍照
(1)CameraActivity.java
package com.camera.test;import android.app.Activity;import android.content.pm.ActivityInfo;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;import android.widget.RelativeLayout;public class CameraActivity extends Activity {private CameraView view;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);requestWindowFeature(Window.FEATURE_NO_TITLE);// 設置橫屏模式以及全屏模式view = new CameraView(this);// 通過一個surfaceview的view來實現拍照view.setId(1);view = new CameraView(this, this);setContentView(R.layout.camera_layout);RelativeLayout relative = (RelativeLayout) this.findViewById(R.id.ly);RelativeLayout.LayoutParams Layout = new RelativeLayout.LayoutParams(3, 3);// 設置surfaceview使其滿足需求無法觀看預覽Layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);Layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);relative.addView(view, Layout);}}(2) CameraView.javapackage com.camera.test;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Point;import android.graphics.PointF;import android.hardware.Camera;import android.hardware.Camera.AutoFocusCallback;import android.hardware.Camera.ShutterCallback;import android.media.AudioManager;import android.media.FaceDetector;import android.media.FaceDetector.Face;import android.os.Environment;import android.os.Handler;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.widget.Toast;public class CameraView extends SurfaceView implements SurfaceHolder.Callback,Camera.PictureCallback{private SurfaceHolder holder;private Camera camera;private Camera.Parameters parameters;private Activity act;private Handler handler = new Handler();private Context context;private SurfaceView surfaceView;private AudioManager audio;private int current;public CameraView(Context context) {super(context);surfaceView = this;audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);final int current = audio.getRingerMode();audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);this.context = context;holder = getHolder();// 生成Surface Holderholder.addCallback(this);holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 指定Push Bufferhandler.postDelayed(new Runnable() {@Overridepublic void run() {if (camera == null) {handler.postDelayed(this, 1 * 1000);// 由於啟動camera需要時間,在此讓其等兩秒再進行聚焦知道camera不為空} else {camera.autoFocus(new AutoFocusCallback() {@Overridepublic void onAutoFocus(boolean success, Camera camera) {if (success) {camera.takePicture(new ShutterCallback() {// 如果聚焦成功則進行拍照@Overridepublic void onShutter() {}}, null, CameraView.this);} else {}}});}}}, 2 * 1000);}public CameraView(Context context, Activity act) {// 在此定義一個構造方法用於拍照過後把CameraActivity給finish掉this(context);this.act = act;}@Overridepublic void surfaceCreated(final SurfaceHolder holder) {// TODO Auto-generated method stubcamera = Camera.open();// 攝像頭的初始化handler.postDelayed(new Runnable() {@Overridepublic void run() {if (holder != null) {try {camera.setPreviewDisplay(holder);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else {handler.postDelayed(this, 1 * 1000);}}}, 2 * 1000);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stubparameters = camera.getParameters();camera.setParameters(parameters);// 設置參數camera.startPreview();// 開始預覽}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stub}public void onPictureTaken(byte[] data, Camera camera) {// 拍攝完成後保存照片try {Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");String time = format.format(date);//在SD卡上創建文件夾File file = new File(Environment.getExternalStorageDirectory()+ "/myCamera/pic");if (!file.exists()) {file.mkdirs();}String path = Environment.getExternalStorageDirectory()+ "/myCamera/pic/" + time + ".jpg";data2file(data, path);camera.setPreviewCallback(null);camera.stopPreview();camera.release();camera = null;holder.removeCallback(CameraView.this);audio.setRingerMode(current);act.finish();//uploadFile(path);} catch (Exception e) {}}private void data2file(byte[] w, String fileName) throws Exception {// 將二進制數據轉換為文件的函數FileOutputStream out = null;try {out = new FileOutputStream(fileName);out.write(w);out.close();} catch (Exception e) {if (out != null)out.close();throw e;}}// private void uploadFile(String filePath)// 拍照過後上傳文件到伺服器// {// }}