前置相机nullnull等产品是什么意思思

当前访客身份:游客 [
错算眉间情深
目前还没有任何评论
今日访问:0
昨日访问:1
本周访问:1
本月访问:28
所有访问:775
相机开发(前后摄像头小笔记)
发表于2年前( 17:30)&&
阅读(442)&|&评论()
0人收藏此文章,
由于应用支持Android 2.2 版本,所以直接使用Camera.open()打开Camera,但据反应在某些平板上无后置摄像头但有前置摄像头的时候,则无法使用(因为open()方法只能打开后置摄像头)。
故使用反射则中解决,默认使用后置。(在台电平板上已测试通过)
* 打开默认相机
* 默认打开后置摄像头,若没有则打开前置
public static Camera openDefaultCamera()
Camera camera =
Class&?& cameraClass = Camera.
Method method = cameraClass.getMethod("getNumberOfCameras");
int cameraNum = (Integer) method.invoke(null);
int backId = -1;
int frontId = -1;
int defaultId = 0;
Class&?&[] classes = cameraClass.getDeclaredClasses();
Class&?& cameraInfoCalss =
for (Class&?& clz : classes)
String name = clz.getName();
if (name.equals("android.hardware.Camera$CameraInfo"))
cameraInfoCalss =
if (cameraInfoCalss == null)
Field backField = cameraInfoCalss.getField("CAMERA_FACING_BACK");
Field frontField = cameraInfoCalss.getField("CAMERA_FACING_FRONT");
//获取前后摄像头默认facing int值
int backFacing = backField.getInt(cameraInfoCalss);
int frontFacing = frontField.getInt(cameraInfoCalss);
java.lang.reflect.Constructor cons = cameraInfoCalss.getConstructor();
if (cons == null)
Object cameraInfo = cons.newInstance();
for (int i = 0; i & cameraN i++)
Method getCameraInfo = cameraClass.getMethod("getCameraInfo", int.class, cameraInfoCalss);
getCameraInfo.invoke(null, i, cameraInfo);
if (cameraInfo == null)
Field field = cameraInfoCalss.getDeclaredField("facing");
int facing = field.getInt(cameraInfo);
if (facing == backFacing)
backId = defaultId;
defaultId++;
else if (facing == frontFacing)
frontId = defaultId;
defaultId++;
Method openMethod = cameraClass.getMethod("open", int.class);
if (backId != -1)
//默认使用后置摄像头
camera = (Camera) openMethod.invoke(null, backId);
else if (frontId != -1)
camera = (Camera) openMethod.invoke(null, frontId);
catch (NoSuchMethodException e)
e.printStackTrace();
catch (InvocationTargetException e)
e.printStackTrace();
catch (IllegalAccessException e)
e.printStackTrace();
catch (InstantiationException e)
e.printStackTrace();
catch (NoSuchFieldException e)
e.printStackTrace();
if (camera == null)
//若为空,使用默认方法打开一次
camera = Camera.open();
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读前置相机null什么意思_百度知道
前置相机null什么意思
没有null[英][n&#652;l][美][n&#652;术&gt.&l]零值的; 失效的; 等于零的; (协议) 无法律效力空
其他类似问题
null的相关知识
按默认排序
其他1条回答
是一种插件吧
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁我是女生,想要买一只手机,像素在800万,最好有前置摄像头,价格在2000及以下的智能手机,大家推荐推荐啊_百度知道
我是女生,想要买一只手机,像素在800万,最好有前置摄像头,价格在2000及以下的智能手机,大家推荐推荐啊
直板使用目的:1000多外观,听音乐:500万以上大家用的好的推荐以下啊,拍照片使用人.0英寸及以上摄像头:智能机:4:Android屏幕尺寸:女性操作系统价格
提问者采纳
很鲜艳:女孩的话我比较喜欢HTC的G11红色这个,已打到国外市场)提示摩托的me525摩托的XT531htc
G13htc G11htc G7htc G12htc G17三星的i9000三星S5830三星I9023魅族MX小米2(国内手机排行榜.总体来说比较适合女生.
提问者评价
谢谢你的耐心解答,好详细呀
其他类似问题
5G 前130W 后800W 参考价格1999元华为U9508(荣耀四核爱享版)四核1.5系统 后800W 参考价格2000元全手打 望采纳 不懂请追问 我将竭诚为您服务
手机购买问题请认准砖家.4G 前130W 后800W 参考价格1888元HTC G11(Incredible S)单核1G 前130W 后800W 参考价格1850元HTC G14(sensation)双核1.4G WP7,以下几款可以参考购买联想乐Phone S720 双核1G
前130W 后800W 参考价格1799元(粉色手机很适合你)中兴U985(Grand Era)四核1.5G 前130W 后800W 参考价格1900元诺基亚800 单核1.2G 前130W 后800W 参考价格1600元摩托罗拉XT882
双核1G 前 30W 后800W 参考价格1700元LG LU6200(Optimus LTE)双核1.2G 前30W 后800W 参考价格1900元三星I997(Infuse 4G)单核1您好
按默认排序
其他7条回答
分辨率高,800W像素2000以下,屏幕大,我比较建议你买Lg6200
魅族MX新双核
要求有点高、、、不过你可以看看诺基亚610
htc t328t 白色
,很不错,今天我入手了一个。。。。
买索尼,不会错。
选择很多啊这个价位miui 啊。华为啊很多啦
您可能关注的推广回答者:
前置摄像头的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁android 后台无声拍照,偷拍, 后置摄像头 照片保存在sd卡的picture文件夹里 ...
查看: 35880|
摘要: 供参考~可用
package com.chenww.camera./**&* 用来拍照的Activity&* @author Chenww&*/import java.io.Fimport java.io.FileOutputSimport java.io.IOEimport java.util.Dimport android.app.Aimport android.content.Cimport android.content.pm.PackageMimport android.graphics.Bimport android.graphics.BitmapFimport android.graphics.Mimport android.hardware.Cimport android.hardware.Camera.AutoFocusCimport android.hardware.Camera.PictureCimport android.os.Bimport android.os.Eimport android.text.format.DateFimport android.util.Limport android.view.SurfaceHimport android.view.SurfaceVimport android.view.Wimport android.view.WindowMimport android.widget.Tpublic class CameraActivity extends Activity { private SurfaceView mySurfaceV private SurfaceHolder myH private Camera myC
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 无title&& & & & requestWindowFeature(Window.FEATURE_NO_TITLE);&& & & & // 全屏&& & & & getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,&& & & & & & & & WindowManager.LayoutParams.FLAG_FULLSCREEN);& & & &&& & & & //设置布局& & & & setContentView(R.layout.activity_camera);& & & &&& & & & Log.d("Demo", "oncreate");& & & &&& & & & //初始化surface& & & & initSurface();& & & &&& & & & //这里得开线程进行拍照,因为Activity还未完全显示的时候,是无法进行拍照的,SurfaceView必须先显示
new Thread(new Runnable() {
public void run() {
//初始化camera并对焦拍照
& & & &initCamera();
}).start();& & & && }
//初始化surface @SuppressWarnings("deprecation") private void initSurface() {
//初始化surfaceview
mySurfaceView = (SurfaceView) findViewById(R.id.camera_surfaceview);
//初始化surfaceholder
myHolder = mySurfaceView.getHolder();
myHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//初始化摄像头 private void initCamera() {
//如果存在摄像头
if(checkCameraHardware(getApplicationContext()))
//获取摄像头(首选前置,无前置选后置)
if(openFacingFrontCamera())
Log.d("Demo", "openCameraSuccess");
//进行对焦
autoFocus();
Log.d("Demo", "openCameraFailed");
//对焦并拍照 private void autoFocus() {
//因为开启摄像头需要时间,这里让线程睡两秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
//自动对焦
myCamera.autoFocus(myAutoFocus);
//对焦后拍照
myCamera.takePicture(null, null, myPicCallback); } //判断是否存在摄像头 private boolean checkCameraHardware(Context context) {
& & & & if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){& & & & & & // 设备存在摄像头& & & & & && & & & } else {& & & & & & // 设备不存在摄像头& & & & & && & & & }& & & &&& & }
//得到后置摄像头& & private boolean openFacingFrontCamera() {& &
//尝试开启前置摄像头
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
//如果开启前置失败(无前置)则开启后置
for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx & cameraC camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
myCamera = Camera.open(camIdx);
} catch (RuntimeException e) {
if (myCamera == null) {
for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx & cameraC camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
Log.d("Demo", "tryToOpenCamera");
myCamera = Camera.open(camIdx);
} catch (RuntimeException e) {
e.printStackTrace();
//这里的myCamera为已经初始化的Camera对象
myCamera.setPreviewDisplay(myHolder);
} catch (IOException e) {
e.printStackTrace();
myCamera.stopPreview();
& & myCamera.release();
& & myCamera =
myCamera.startPreview();
}& && //自动对焦回调函数(空实现) private AutoFocusCallback myAutoFocus = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
//拍照成功回调函数 private PictureCallback myPicCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//完成拍照后关闭Activity
CameraActivity.this.finish();
tmpFile = new File(Environment.getExternalStorageDirectory().getCanonicalFile()+"/Picture");
if(!tmpFile.exists()){
tmpFile.mkdirs();
} catch (IOException e1) {
e1.printStackTrace();
String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString()+".jpg";
File picture = new File(Environment.getExternalStorageDirectory()+"/Picture/"+fname);
FileOutputStream fos = new FileOutputStream(picture.getPath()); // Get file output stream//
fos.write(params[0]); // Written to the file//
fos.close();&//
} catch (Exception e) {//
e.printStackTrace();//
FuncPhoto.this.finish();
//创建并保存图片文件
//File pictureFile = new File(getDir(), "camera.jpg");
//将得到的照片进行270°旋转,使其竖直
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix matrix = new Matrix();
matrix.preRotate(90);
bitmap = Bitmap.createBitmap(bitmap ,0,0, bitmap .getWidth(), bitmap .getHeight(),matrix,true);
& & & &try {
& & & & & &FileOutputStream fos = new FileOutputStream(picture.getPath());
& & & & & &pressFormat.PNG, 100, fos);
& & & & & &fos.close();
& & & &} catch (Exception error) {
& & & & Toast.makeText(CameraActivity.this, "拍照失败", Toast.LENGTH_SHORT).show();;
& & & & Log.d("Demo", "保存照片失败" + error.toString());
& & & & error.printStackTrace();
& & & & myCamera.stopPreview();
& & myCamera.release();
& & myCamera =
& & Log.d("Demo", "获取照片成功");
& & Toast.makeText(CameraActivity.this, "获取照片成功", Toast.LENGTH_SHORT).show();;
myCamera.stopPreview();
myCamera.release();
myCamera =
//获取文件夹 private File getDir() {
//得到SD卡根目录
File dir = Environment.getExternalStorageDirectory();
if (dir.exists()) {
dir.mkdirs();
} }}mainfest里加入权限:&& &&!-- 调用相机权限 --&
& & &uses-permission android:name="android.permission.CAMERA" /&
& & &uses-feature android:name="android.hardware.camera" /&
& & &uses-feature android:name="android.hardware.camera.autofocus" /&
& & &!-- 读写SD卡权限 --&
& & &uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&mainfest里activity透明&&!-- 拍照界面Activity --&
& & & & &activity
& & & & & & android:name="com.chenww.camera.ui.CameraActivity"
& & & & & & android:theme="@android:style/Theme.Translucent" &
& & & & & & &!-- theme设置成全透明 --&
& & & & & & &intent-filter&
& & & & & & & & &action android:name="android.intent.action.MAIN" /&
& & & & & & & & &category android:name="android.intent.category.LAUNCHER" /&
& & & & & & &/intent-filter&
& & & & &/activity&activity_camera.xml如下:&RelativeLayout xmlns:android="/apk/res/android"
& & xmlns:tools="/tools"
& & android:layout_width="match_parent"
& & android:layout_height="match_parent"
& & tools:context=".MyCamera" &
& & &SurfaceView
& & & & android:id="@+id/camera_surfaceview"
& & & & android:layout_width="0.1dp"
& & & & android:layout_height="0.1dp" &
& & &/SurfaceView&
&/RelativeLayout&
上一篇:下一篇:
Powered by

我要回帖

更多关于 利息前置什么意思 的文章

 

随机推荐