天天看点

android中利用Java反射机制获取系统相机对象

JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。

能够使用反射的前提是在虚拟机中已经加载了要反射的类,相对于在编码时的静态引用,反射是在程序运行时动态调用的。

尽管在这样的定义与分类下Java不是动态语言,它却有着一个非常突出的动态相关机制:Reflection。这个字的意思是“反射、映象、倒影”,用在Java身上指的是我们可以于运行时加载、探知、使用编译期间完全未知的classes。换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods。这种“看透class”的能力(the ability of the program to examine itself)被称为introspection(内省、内观、反省)。Reflection和introspection是常被并提的两个术语。

和反射相关的包名java.lang.Class,以及java.lang.reflect

if (Build.VERSION.SDK_INT > 10) {
            try {
                Class<?> clsCamera = Camera.class;
                Method staticMethodOpen = clsCamera
                        .getMethod("open", int.class);
                mCamera = (Camera) staticMethodOpen.invoke(clsCamera, 0);
            } catch (IllegalArgumentException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (SecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (NoSuchMethodException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else {
            try {
                if (mCamera == null)
                    mCamera = Camera.open();
            } catch (Exception e) {
                return;
            }
        }
        if (mCamera == null) {
            return;
        }
        Parameters parameters = mCamera.getParameters();
        List<Camera.Size> previewSize = parameters.getSupportedPreviewSizes();
        Camera.Size previewSizes = previewSize.get(0);
        parameters.setPreviewSize(previewSizes.width, previewSizes.height);
        parameters.setPreviewFrameRate(5);
        List<Camera.Size> pictureSize = parameters.getSupportedPictureSizes();
        Camera.Size pictureSizes = pictureSize.get(0);
        parameters.setPictureSize(pictureSizes.width, pictureSizes.height);
        mCamera.setParameters(parameters);
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
        }

if (Build.VERSION.SDK_INT > 10) {
                try {
                    Class<?> clsCamera = Camera.class;
                    Method staticMethodOpen = clsCamera.getMethod("open",
                            int.class);
                    mCamera = (Camera) staticMethodOpen.invoke(clsCamera, 0);
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SecurityException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (NoSuchMethodException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } else {
                try {
                    if (mCamera == null)
                        mCamera = Camera.open();
                } catch (Exception e) {
                    return;
                }
            }

            if (mCamera == null) {
                return;
            }
            Parameters parameters = mCamera.getParameters();
            List<Camera.Size> previewSize = parameters
                    .getSupportedPreviewSizes();
            Camera.Size previewSizes = previewSize.get(0);
            parameters.setPreviewSize(previewSizes.width, previewSizes.height);
            parameters.setPreviewFrameRate(5);
            List<Camera.Size> pictureSize = parameters
                    .getSupportedPictureSizes();
            Camera.Size pictureSizes = pictureSize.get(0);
            parameters.setPictureSize(pictureSizes.width, pictureSizes.height);
            mCamera.setParameters(parameters);
            try {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            } catch (IOException e) {
            }
           

继续阅读