How to open Camera in Portrait Mode using OpenCV in Android Application

后端 未结 2 559
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 11:04

I am using OpveCV 2.4.7 library When i use OpenCV in Android Application for open Camera then it shows the camera in Landscape mode. I trie

2条回答
  •  离开以前
    2021-01-18 11:19

    You can do this. Make a JavaCamResView class which extends JavaCameraView of OPENCV. The you get camera Controls. import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List;

                import org.opencv.android.JavaCameraView;
                import org.opencv.core.Size;
    
    
                import android.content.Context;
                import android.hardware.Camera;
                import android.util.AttributeSet;
                import android.util.Log;
                import android.view.MenuItem;
                import android.widget.Toast;
    
                public class JavaCamResView extends JavaCameraView {
    
                    public JavaCamResView(Context context, AttributeSet attrs) {
                        super(context, attrs);
    
                    }
    
                    public List getResolutionList() {
                        return  mCamera.getParameters().getSupportedPreviewSizes();
                    }
    
                    public void setResolution(Camera.Size resolution) {
                        disconnectCamera();
                        connectCamera((int)resolution.width, (int)resolution.height);
                    }
    
                    public void setFocusMode (Context item, int type){
    
                        Camera.Parameters params = mCamera.getParameters();
    
                        List FocusModes = params.getSupportedFocusModes();
    
                        switch (type){
                            case 0:
                                if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
                                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                                else
                                    Toast.makeText(item, "Auto Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 1:
                                if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
                                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
                                else
                                    Toast.makeText(item, "Continuous Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 2:
                                if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_EDOF))
                                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_EDOF);
                                else
                                    Toast.makeText(item, "EDOF Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 3:
                                if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_FIXED))
                                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
                                else
                                    Toast.makeText(item, "Fixed Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 4:
                                if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_INFINITY))
                                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
                                else
                                    Toast.makeText(item, "Infinity Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 5:
                                if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_MACRO))
                                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
                                else
                                    Toast.makeText(item, "Macro Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                        }
    
                        mCamera.setParameters(params);
                    }
    
                    public void setFlashMode (Context item, int type){
    
                        Camera.Parameters params = mCamera.getParameters();
                        List FlashModes = params.getSupportedFlashModes();
    
                        switch (type){
                            case 0:
                                if (FlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO))
                                    params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
                                else
                                    Toast.makeText(item, "Auto Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 1:
                                if (FlashModes.contains(Camera.Parameters.FLASH_MODE_OFF))
                                    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                                else
                                    Toast.makeText(item, "Off Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 2:
                                if (FlashModes.contains(Camera.Parameters.FLASH_MODE_ON))
                                    params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
                                else
                                    Toast.makeText(item, "On Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 3:
                                if (FlashModes.contains(Camera.Parameters.FLASH_MODE_RED_EYE))
                                    params.setFlashMode(Camera.Parameters.FLASH_MODE_RED_EYE);
                                else
                                    Toast.makeText(item, "Red Eye Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                            case 4:
                                if (FlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH))
                                    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                                else
                                    Toast.makeText(item, "Torch Mode not supported", Toast.LENGTH_SHORT).show();
                                break;
                        }
    
                        mCamera.setParameters(params);
                    }
    
                    public Camera.Size getResolution() {
    
                        Camera.Parameters params = mCamera.getParameters();
    
                        Camera.Size s = params.getPreviewSize();
                        return s;
                    }
    
                    public void zoomIn()
                    {
    
                        Camera.Parameters params = mCamera.getParameters();
                        if (params.isZoomSupported()) {
                            final int maxZoomLevel = params.getMaxZoom();
                            int currentZoomLevel = params.getZoom();
                            Log.i("max ZOOM ", "is " + maxZoomLevel);
                            Log.i("current ZOOM ", "is " + currentZoomLevel);
    
                            if(currentZoomLevel < maxZoomLevel){
                                currentZoomLevel++;
                                params.setZoom(currentZoomLevel);
                                mCamera.setParameters(params);
    
                            }
    
                        }
                        else
                        {
                            Log.i("ZOOM " , "NOT Supported");
    
                        }
    
                    }
    
                    public int getExposure()
                    {
                        Camera.Parameters params = mCamera.getParameters();
                        return params.getExposureCompensation();
                    }
                    public int getMinExposure()
                    {
                        Camera.Parameters params = mCamera.getParameters();
                        return params.getMinExposureCompensation();
                    }
                    public int getMaxExposure()
                    {
                        Camera.Parameters params = mCamera.getParameters();
                        return params.getMaxExposureCompensation();
                    }
                    public void setExposure(int exposure)
                    {
                            Camera.Parameters params = mCamera.getParameters();
                            params.setExposureCompensation(exposure);
                            mCamera.setParameters(params);
    
                        }
    
                    public void setZoom(int zoom_value)
                    {
    
                        Camera.Parameters params = mCamera.getParameters();
                        if (params.isZoomSupported()) {
                            final int maxZoomLevel = params.getMaxZoom();
                            int currentZoomLevel = params.getZoom();
    
    
                          //  Log.i("CAMERA_SETTING", "Exposure setting = " + params.get("exposure"));
                            Log.i("CAMERA_SETTING", "Current Exposure Compansation = " + params.getExposureCompensation());
                            Log.i("CAMERA_SETTING", "min Exposure Compansation = " + params.getMinExposureCompensation());
                            Log.i("CAMERA_SETTING", "Max Exposure Compansation = " + params.getMaxExposureCompensation());
                            Log.i("CAMERA_SETTING", "White Balance setting = " + params.get("whitebalance"));
                            Log.i("CAMERA_SETTING", "Supported White Balance Modes:" + params.get("whitebalance-values"));
                            if(zoom_value < maxZoomLevel){
                                params.setZoom(zoom_value);
                                mCamera.setParameters(params);
    
                            }
    
                        }
                        else
                        {
                            Log.i("ZOOM " , "NOT Supported");
    
                        }
    
                    }
                    public void zoomOut()
                    {
    
                        Camera.Parameters params = mCamera.getParameters();
                        if (params.isZoomSupported()) {
    
                            int currentZoomLevel = params.getZoom();
                            Log.i("current ZOOM ", "is " + currentZoomLevel);
    
                            if(currentZoomLevel > 0 ){
                                currentZoomLevel--;
                                params.setZoom(currentZoomLevel);
                                mCamera.setParameters(params);
    
                            }
    
                        }
                        else
                        {
                            Log.i("ZOOM " , "NOT Supported");
    
                        }
    
                    }
    
                    public void getPictureSize()
                    {
                        Camera.Parameters params = mCamera.getParameters();
                        List sizes = params.getSupportedPreviewSizes();
                        Camera.Size mSize;
                        for (Camera.Size size : sizes) {
                            Log.i("CAMERA_SIZE", "Available resolution: "+size.width+" "+size.height);
                            mSize = size;
                        }
                        params.setPictureSize(1280 , 768);
                        mCamera.setParameters(params);
                    }
    
                    public void getOptimalPreviewSize( int w, int h) {
                        final double ASPECT_TOLERANCE = 0.1;
                        double targetRatio=(double)h / w;
    
                        Camera.Parameters params = mCamera.getParameters();
                        List sizes = params.getSupportedPreviewSizes();
    
                        if (sizes == null) return;
    
                        Camera.Size optimalSize = null;
                        double minDiff = Double.MAX_VALUE;
    
                        int targetHeight = h;
    
                        for (Camera.Size size : sizes) {
                            double ratio = (double) size.width / size.height;
                            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
                            if (Math.abs(size.height - targetHeight) < minDiff) {
                                optimalSize = size;
                                minDiff = Math.abs(size.height - targetHeight);
                            }
                        }
    
                        if (optimalSize == null) {
                            minDiff = Double.MAX_VALUE;
                            for (Camera.Size size : sizes) {
                                if (Math.abs(size.height - targetHeight) < minDiff) {
                                    optimalSize = size;
                                    minDiff = Math.abs(size.height - targetHeight);
                                }
                            }
                        }
                        Log.i("CAMERA_SIZE", "Optimal Size: "+optimalSize.width+" "+optimalSize.height);
                    }
    
                    public void changeOrientation() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
                        Method rotateMethod;
                        rotateMethod = android.hardware.Camera.class.getMethod("setDisplayOrientation", int.class);
                        rotateMethod.invoke(mCamera, 90);
                    }
    
    
    
    
                }
    

提交回复
热议问题