天天看點

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) {
            }
           

繼續閱讀