导航:首页 > 操作系统 > android摄像头

android摄像头

发布时间:2022-02-06 14:21:38

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)// 拍照过后上传文件到服务器// {// }}

阅读全文

与android摄像头相关的资料

热点内容
r1234yf汽车空调压缩机 浏览:143
ftp服务器地址栏 浏览:898
linux图形分区 浏览:963
安徽到辽宁源码 浏览:575
libs安卓的文件夹叫什么 浏览:869
生意圈app是什么意思 浏览:395
linuxarcgisserver 浏览:234
加密pdf怎么修改文件 浏览:138
红米刷机无命令怎么办 浏览:356
啥叫美国谷歌外包程序员 浏览:260
云服务器管家婆 浏览:440
发邮件命令 浏览:354
程序员好做吗工作好吗 浏览:886
云电脑服务器维护一个月多少钱 浏览:882
有没有什么app数学题型较多 浏览:341
政策pdf 浏览:295
有什么好玩的文娱app 浏览:811
python教学合集 浏览:959
有什么好用的小众app吗 浏览:118
芋道app源码 浏览:448