Android camera preview look strange

后端 未结 4 976
醉梦人生
醉梦人生 2020-12-11 05:41

I am implementing a camera app and when I look at the preview (especially with front camera), the image is very fat. It looks like the image get stretched horizontally. I fo

4条回答
  •  自闭症患者
    2020-12-11 05:51

    I already solved problem. What could cause problem with strange camera preview.

    • Status bar takes spaces - you can hide it Hiding the Status Bar

    • Some spaces also take TitleBar - you can turn off this at manifest

    android:theme="@android:style/Theme.NoTitleBar">

    • Changed activity orientation to landscape "beacuse camera preview support that orientation" - you can check this at API demo Graphics->CameraPreview

    • algorithm that compares the size of Display.getWidth () Camera.getParameters size (). getSupportedPreviewSizes (); if they are the same is a function of surfaceChanged change Parametrs.setPreviewSize (x, y) you received when searching the list


    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters setPrevOrientation = mCamera.getParameters();
        if (mHolder.getSurface() == null)
            return;
    
        try {
            mCamera.stopPreview();
            Size sizeBefore = mCamera.getParameters().getPreviewSize();
            setPrevOrientation.setRotation(setCameraDisplayOrientation(
                    (Activity) context, getCameraId(), mCamera));
            // Orientacja Portrait np 640x480 Landscape 480x640
            this.resolution.setPreviewSize(this.optimalPreviewSize.x,
                    this.optimalPreviewSize.y);
            mCamera.setParameters(this.resolution);
            Size sizeAfter = mCamera.getParameters().getPreviewSize();
        } catch (RuntimeException e) {
    
            L.d("Podgląd nie istnieje");
        }
    
        try {
            mCamera.stopPreview();
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
    
        } catch (Exception e) {
            L.d("błąd podgladu: " + e.getMessage());
        }
     } 
    

        private Point getOptimalPreviewResolution(Display displaySize) {
    
            lSuportedPreviewSize = mCamera.getParameters()
                    .getSupportedPreviewSizes();
            Point optimalPreviewSize = new Point();
            int displayWidth = displaySize.getWidth();
            int displayHeight = displaySize.getHeight();
            int cameraHeight;
            int cameraWidth;
    
            List lOptimalPoint = new ArrayList();
    
            for (int i = 0; i < lSuportedPreviewSize.size(); i++) {
                cameraHeight = lSuportedPreviewSize.get(i).width;
                cameraWidth = lSuportedPreviewSize.get(i).height;
                if (displayHeight >= cameraHeight) {
                    lOptimalPoint.add(new Point(cameraHeight, cameraWidth));
                }
            }
    
            // Sort ascending
            Collections.sort(lOptimalPoint,
                    new ComapreSupportedPreviewByWidth());
            // Last element is optimal
            optimalPreviewSize = lOptimalPoint.get(lOptimalPoint.size() - 1);
    
            // Return resolution - camera at landscape mode (800x600)
            return optimalPreviewSize;
        }
    

        class ComapreSupportedPreviewByWidth implements Comparator {
    
        @Override
        public int compare(Point lhs, Point rhs) {
            return lhs.x - rhs.x;
        }
    

提交回复
热议问题