How to record video of particular width and height on samsung device android?

后端 未结 2 614
失恋的感觉
失恋的感觉 2020-12-15 22:38

Hello I am making an android application in which I am using custom camera for recording camera.I am having problem on samsung device.I can not set the profile of Media reco

相关标签:
2条回答
  • 2020-12-15 23:22

    With using the parameters you can set the video preview width and height..

            Camera.Parameters param = camera.getParameters();
    
            param.setPreviewSize("your width"
                    "your height");
            camera.setParameters(param);
    
    0 讨论(0)
  • 2020-12-15 23:35

    This happens because you are requesting the dimensions that you want, but not everycamera is capable of every dimension.

    Besides, some devices works with camera aspect ratios slightly diferent, so if you are requesting a rectangle with a wrong ratio, or with a dimensions diferent from the supported, it will crash in some devices.

    What to do?

    Step 1.

    you have to check for the supported sizes. You can do it with

    Camera.Parameters p = myCamera.getParameters(); 
    List<Size> previewsizes = p.getSupportedPreviewSizes();
    List<Size> videosizes = p.getSupportedVideoSizes();
    

    and then, you can choose one. If you want to automatize this, you can go further, and follow the

    Step 2

    write a function to select the best available size, which will receive the supported sizes, and the desired size, something like:

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
            final double ASPECT_TOLERANCE = 0.2;
            double targetRatio = (double) w / h;
            if (sizes == null)
                return null;
    
            Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;
    
            int targetHeight = h;
    
            // Try to find an size match aspect ratio and size
            for (Size size : sizes) {
                Log.d("Camera", "Checking size " + size.width + "w " + size.height
                        + "h");
                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);
                }
            }
    
            // Cannot find the one match the aspect ratio, ignore the
            // requirement
            if (optimalSize == null) {
                minDiff = Double.MAX_VALUE;
                for (Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }
            }
            return optimalSize;
        }
    

    And the last step, set the parameters

    Step 3

    private int desiredwidth=640, desiredheight=360; 
    
    Size optimalPreviewSize = getOptimalPreviewSize(previewsizes, desiredwidth, desiredheight);         
    
    Size optimalVideoSize = getOptimalPreviewSize(videosizes, desiredwidth, desiredheight);      
    
    p.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height);
    
     mCamera.unlock();
     mMediaRecorder = new MediaRecorder();
     mMediaRecorder.setCamera(mCamera);
    
     mMediaRecorder.setVideoSize(optimalVideoSize.width, optimalVideoSize.height);
     myCamera.setParameters(p);
    

    With this, your camera app will work in every device with a camera!

    UPDATE

    With the getOptimalPreviewSize that i wrote, you get the size whose ratio is closer to the desired, and if none is good enough, you get the one which height is closed to the desired. if you want to give more importante to the size, you can make an easy change, something like

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.2;
        double targetRatio = (double) w / h;
        if (sizes == null)
            return null;
    
        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;
    
        int targetHeight = h;
    
       if (  you want ratio as closed to what i asked for)
       {  for (Size size : sizes) {
            Log.d("Camera", "Checking size " + size.width + "w " + size.height
                    + "h");
            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 (you want height as closed to what i asked for) { //you can do other for width
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
    
       if (you want the bigest one) { 
           minDiff = 0;
           for (Size size : sizes) {
               if (  size.height * size.width > minDiff ) {
                   optimalSize = size;
                   minDiff = size.height * size.width ;
               }
           }
       }
      return optimalSize;
    }
    
    0 讨论(0)
提交回复
热议问题