android 调用前摄像头进行拍照的方法及完整例子

android调用camera时,可以自己写一个activity,赋上相关参数,打开前camera就可以了;

需要申请的permission,在AndroidManifest.xml中添加:

        <uses-permission android:name="android.permission.CAMERA" />
	<uses-feature android:name="android.hardware.camera" android:required="false" />
	<uses-feature android:name="android.hardware.camera.front" android:required="false" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

主要功能,打开前camera

	private Camera openFrontFacingCameraGingerbread() {
	    int cameraCount = 0;
	    Camera cam = null;
	    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
	    cameraCount = Camera.getNumberOfCameras();

	    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
	        Camera.getCameraInfo(camIdx, cameraInfo);
	        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
	            try {
	                cam = Camera.open(camIdx);
	                mCurrentCamIndex = camIdx;
	            } catch (RuntimeException e) {
	                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
	            }
	        }
	    }

	    return cam;
	}

根据打开时的横竖屏方向来调整preview角度

	//根据横竖屏自动调节preview方向,Starting from API level 14, this method can be called when preview is active.
	private static void setCameraDisplayOrientation(Activity activity,int cameraId, Camera camera)
	{
		   Camera.CameraInfo info = new Camera.CameraInfo();
	       Camera.getCameraInfo(cameraId, info);
	       int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

	       //degrees  the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270.
	       //The starting position is 0 (landscape).
	       int degrees = 0;
	       switch (rotation)
	       {
	           case Surface.ROTATION_0: degrees = 0; break;
	           case Surface.ROTATION_90: degrees = 90; break;
	           case Surface.ROTATION_180: degrees = 180; break;
	           case Surface.ROTATION_270: degrees = 270; break;
	        }
	       int result;
	       if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
	       {
	           result = (info.orientation + degrees) % 360;
	           result = (360 - result) % 360;  // compensate the mirror
	       }
	       else
	       {
	       // back-facing
	          result = (info.orientation - degrees + 360) % 360;
	       }
	       camera.setDisplayOrientation(result);
	} 

本博文关联的源码下载:http://download.csdn.net/detail/changliangdoscript/7446287

android 调用前摄像头进行拍照的方法及完整例子

时间: 2024-08-06 11:57:47

android 调用前摄像头进行拍照的方法及完整例子的相关文章

纯JavaScript实现的调用设备摄像头并拍照的功能

这篇文章本来不在Jerry计划内的,咱们SAP中国研究院今天已经正式上班了,Jerry也回到工作岗位开始搬砖了. 今天一位同事问我关于本文标题描述的功能如何实现,Jerry在网上随便搜了一下,类似的例子非常多,这里随便找了一个例子做了精简,方便Jerry以后重用. 其实之前Jerry的文章 只要200行JavaScript代码,就能把特斯拉汽车带到您身边,里面使用到的React-Native加上ViroReact的组合,也能实现用JavaScript调用手机摄像头并拍照的功能,不过那个应用是通过

Android调用前置摄像头的方法

之前在网上找到到好几种办法,发现要么不行要么太麻烦,现在提供一种比较方便简单地方法,在此记录一下: Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra("android.intent.extras.CAMERA_FACING", 1); // 调用前置摄像头 startActivityForResult(intent, 1);

Android调用系统摄像头拍照并剪裁压缩

由于业务需要写了一个Android手机拍照的功能Demo,同时实现了图片剪裁和图片压缩.以下是源码 package com.klp.demo_025; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import android.app.Activity; import android.c

Android调用手机摄像头使用MediaRecorder录像并播放

最近在项目开发中需要调用系统的摄像头录像并播放. 在开发中遇到了两个问题,记录下: (1)开发过程中出现摄像头占用,启动失败,报错.但是我已经在onDestory()中关闭了资源. 报错原因:打开程序,调用摄像头,按Home键再打开程序调用,报错摄像头被占用. 解决:在onStop()中关闭资源,在onResume()中判断是否为null,否则实例化资源. (2)其中我录像播放的代码写在Fragment+ViewPager中,在来回切换Fragment的时候,摄像头只能调用一次,并且所在的Fra

wp-query调用前几篇文章的方法

---恢复内容开始--- 利用强大的wp-query函数调用指定分类下的前几篇文章,下面的代码表示调用的是分类ID4下的前两篇文章. <?php $cunt_wenzhen = array('cat' =>'4' , //调用的分类4,可以修改分类id 'posts_per_page' =>'2' ,//显示的前两篇文章,可以修改显示篇数 ); $the_query = new WP_Query( $cunt_wenzhen); // 开始循环 if ( $the_query->h

Android 调用资源字符串的几种方法

在 Layout XML 调用字符串资源: <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> 在 Activity 获取字符串资源: this.getString(R.string.hello); 从 Context 获取字符串资源: context.getS

Android获取前置摄像头实现静默拍照

Android获取前置摄像头静默拍照 在实现入侵警报功能时需要获取系统前置摄像头实现后台静默拍照,并将数据存储到数据库中以备用户查看.具体步骤如下: 1.获取使用相机权限 ActivityCompat.requestPermissions(SomeActivity.this,new String[]{Manifest.permission.CAMERA},1); 2.在拍照所在activity对应的xml文件中添加宽高为0.1dp的SurfaceView <SurfaceView app:lay

android: 调用摄像头拍照

很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片 作为用户的头像,这时打开摄像头拍张照是最简单快捷的.下面就让我们通过一个例子来学 习一下,如何才能在应用程序里调用手机的摄像头进行拍照. 新建一个 ChoosePicTest 项目,然后修改 activity_main.xml 中的代码,如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro

android调用摄像头拍照

调用手机摄像头拍照,获取拍照后的图片数据.以下代码是在activity中:     // 调用摄像头         Button b = (Button) findViewById(R.id.btn1);     b.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             // Here we fire the inte