6

我正在实现一个相机应用程序,当我查看预览时(尤其是前置摄像头),图像非常胖。看起来图像被水平拉伸。我按照优化相机尺寸的 sdk 示例进行操作,但没有帮助。如何调整我的相机设置,以便它可以像其他相机应用程序一样预览?

谢谢。

我的代码如下。

public class CameraActivity extends Activity implements SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback {

Camera m_camera;
SurfaceView m_surfaceView;
int m_numOfCamera;
int m_defaultCameraId;
int m_currentCamera;
int m_surfaceWidth;
int m_surfaceHeight;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    getActionBar().setDisplayHomeAsUpEnabled(true);


    m_surfaceView = (SurfaceView)findViewById(R.id.cameraPreview);
    m_surfaceView.getHolder().addCallback(this);

    m_camera = Camera.open();

    m_numOfCamera = Camera.getNumberOfCameras();

    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < m_numOfCamera; ++i) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
            m_defaultCameraId = i;
            m_currentCamera = m_defaultCameraId;
        }       
    }

    if (m_numOfCamera < 1) {
        MenuItem switchCam = (MenuItem)findViewById(R.id.menu_switch_camera);
        switchCam.setVisible(false);
    }
}

@Override
public void onPause() {
    super.onPause();
    m_camera.stopPreview();
}

@Override
public void onDestroy() {
    super.onDestroy();
    m_camera.release();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_camera, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(final MenuItem item) 
{
    if (item.getItemId() == android.R.id.home) 
    {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

        return true;
    } 
    else if (item.getItemId() == R.id.menu_switch_camera)
    {
        if (m_camera != null) {
            m_camera.stopPreview();
            m_camera.release();
            m_camera = null;
        }   

        m_camera = Camera.open((m_currentCamera + 1) % m_numOfCamera);
        m_currentCamera = (m_currentCamera + 1) % m_numOfCamera;

        Camera.Parameters params = m_camera.getParameters();
        List<Camera.Size> sizes = params.getSupportedPreviewSizes();
        Camera.Size size = getOptimalPreviewSize(sizes, m_surfaceWidth, m_surfaceHeight);

        params.setPreviewSize(size.width,  size.height);

        m_camera.setParameters(params);
        setCameraDisplayOrientation(this, m_currentCamera, m_camera);
        m_camera.startPreview();
        try {
            m_camera.setPreviewDisplay(m_surfaceView.getHolder());
        } catch (Exception e) {
            e.printStackTrace();
        }   
        return true;
    }
    return true;
}

public void onPictureTaken(byte[] arg0, Camera arg1) {
    // TODO Auto-generated method stub

}

public void onShutter() {
    // TODO Auto-generated method stub

}

public void surfaceChanged(SurfaceHolder arg0, int format, int w, int h) {
    m_surfaceWidth = w;
    m_surfaceHeight = h;
    Camera.Parameters params = m_camera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size selected = getOptimalPreviewSize(sizes, w, h);

    params.setPreviewSize(selected.width,  selected.height);


    m_camera.setParameters(params);
    setCameraDisplayOrientation(this, m_currentCamera, m_camera);
    m_camera.startPreview();    
}

private static void setCameraDisplayOrientation(Activity activity,
         int cameraId, android.hardware.Camera camera) {
     android.hardware.Camera.CameraInfo info =
             new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     int rotation = activity.getWindowManager().getDefaultDisplay()
             .getRotation();
     int degrees = 0;
     switch (rotation) {
         case Surface.ROTATION_0: degrees = 0; break;
         case Surface.ROTATION_90: degrees = 90; break;
         case Surface.ROTATION_180: degrees = 180; break;
         case Surface.ROTATION_270: degrees = 270; break;
     }

     int result;
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
         result = (info.orientation + degrees) % 360;
         result = (360 - result) % 360;  // compensate the mirror
     } else {  // back-facing
         result = (info.orientation - degrees + 360) % 360;
     }
     camera.setDisplayOrientation(result);
 }


public void surfaceCreated(SurfaceHolder arg0) {
    try {
        m_camera.setPreviewDisplay(m_surfaceView.getHolder());
    } catch (Exception e) {
        e.printStackTrace();
    }   
}

public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {


    final double ASPECT_TOLERANCE = 0.1;
    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) {
        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;
}

}
4

4 回答 4

16

相机预览总是填满SurfaceView显示它。如果 的纵横比m_surfaceView与相机的纵横比不匹配,则预览将被拉伸。

您需要创建m_surfaceView匹配的纵横比。这意味着,您需要从代码创建它,而不是从布局 XML 文件。

您可以在 android 示例项目中找到一个示例项目APIDemos 。在项目中有一个名为CameraPreview. 这个有一个很好的演示,用于在SurfaceView. 它有一个类 that ,并从代码中extends ViewGroup添加作为它的子项。SurfaceViewonMeasure()方法已被覆盖以确定 的高度和宽度SurfaceView,因此保留了纵横比。看看这个项目,我希望它会很清楚。

[抱歉,我无法在此处发布链接 -应该是链接,但我发现它已损坏。但是如果您已经安装了带有 Android SDK 的示例项目,您可以在示例中找到该项目。打开一个新的 Android 示例项目,选择 APIDemos,然后查找名为CameraPreview. com.example.android.apis.graphics据我记得,它应该在包装里。]

于 2012-10-05T17:29:31.033 回答
1

我改变了 onLayout 方法,而不是相机预览没有被拉伸。其余部分与 APiDemo 相同,可在此处找到 sdk/sample/adroid-18。我们的想法是我们只有一些受支持的预览大小,但我们的视图大小可能并不总是与预览大小匹配。所以我采用了更大的预览尺寸然后是我的 imageview 尺寸。这个对我有用。可以帮助某人..

@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed && getChildCount() > 0) {
            final View child = getChildAt(0);

            final int width = r - l;
            final int height = b - t;

            int previewWidth = width;
            int previewHeight = height;
            if (mPreviewSize != null) {
                previewWidth = mPreviewSize.width;
                previewHeight = mPreviewSize.height;
            }

            // Center the child SurfaceView within the parent.
            if (width * previewHeight < height * previewWidth) {

                final int scaledChildWidth = previewWidth * height
                        / previewHeight;

                left = (width - scaledChildWidth) / 2;
                top = 0;
                right = (width + scaledChildWidth) / 2;
                bottom = height;

                child.layout(left, top, right, bottom);
            } else {
                final int scaledChildHeight = previewHeight * width
                        / previewWidth;

                left = 0;
                top = (height - scaledChildHeight) / 2;
                right = width;
                bottom = (height + scaledChildHeight) / 2;

                child.layout(left, top, right, bottom);
            }
        }
    }
于 2014-01-21T11:26:39.900 回答
0

我对过度拉伸相机预览有疑问。在垂直和横向模式下太拉伸了。

所以在清单中我添加了 screenOrentation="Portrait",但它没有帮助,仍然在任何位置重新缩放预览(垂直 - 预览太宽或横向太长)你可以在屏幕上看到这个。我想在三星 ace III 上添加一切都很好,但在 LG Nexus 4 正在伸展

 package pl.probs.camera.component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import pl.probs.lib.debug.L;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "CameraPreview";
    private static boolean showLogs = true;
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private Context context;
    private Parameters resolution;
    private List<Size> lSuportedPreviewSize;
    private static int cOrientation = 0; // aktualny kat orientacji
    private static boolean cOrientationChanged = false; // Stan orientacji
                                                        // zostal zmieniony
                                                        // wzgledem poprzedniego
    private Display display; // Rozmiar ekranu
    private Point displaySize; // Zmienna przechowuje Rozmiar Ekranu
    private Point optimalPreviewSize;
public CameraPreview(Context context, Camera camera, int resolution) {
    super(context);
    this.optimalPreviewSize = new Point();
    this.context = context;
    this.mCamera = camera;
    setDisplaySize(this.display);
    setFocusable(true);
    setFocusableInTouchMode(true);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    this.resolution = getMinResolution(resolution);
    this.optimalPreviewSize = getOptimalPreviewResolution(this.display);
    Size s = mCamera.getParameters().getPreviewSize(); // Sprawdzenie jaki
                                                        // prewiev ustawiony
}

public Point getOptimalPreviewSize() {
    return optimalPreviewSize;
}

public void surfaceCreated(SurfaceHolder holder) {
    try {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.setPreviewDisplay(holder);
            Size s = mCamera.getParameters().getPreviewSize();
            mCamera.startPreview();
        }
    } catch (IOException e) {
        L.d("Błąd ustawiania podglÄ…du: " + e.getMessage());
    }
}

protected void onPause() {
    // Because the Camera object is a shared resource, it's very
    // important to release it when the activity is paused.
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    if (mCamera != null) {
        mCamera.stopPreview();
    }
}

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());
    }
}

@SuppressLint("ClickableViewAccessibility")
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mCamera.autoFocus(new AutoFocusCallback() {

            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                // do something
            }
        });

    }

    return true;
}

private static int setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();

    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    cOrientation = degrees;

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }

    camera.setDisplayOrientation(result);
    return result;
}

private int getCameraId() {
    int cameraId = -1;
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

private Parameters getMinResolution(int desireResolutionInMpx) {
    int height[], width[], size;
    float megapixels;
    Camera.Parameters p = mCamera.getParameters();
    size = p.getSupportedPictureSizes().size();
    height = new int[size];
    width = new int[size];
    for (int i = 0; i < size; i++) {
        height[i] = p.getSupportedPictureSizes().get(i).height;
        width[i] = p.getSupportedPictureSizes().get(i).width;
        megapixels = (float) (((float) height[i] * (float) width[i]) / 1024000);
        if (megapixels <= desireResolutionInMpx) {
            p.setPictureSize(width[i], height[i]);
            break;
        }
    }
    return p;
}

private Point getOptimalPreviewResolution(Display displaySize) {
    lSuportedPreviewSize = mCamera.getParameters().getSupportedPreviewSizes();
    Point optimalPreviewSize = new Point();
    int displayWidth = displaySize.getWidth(); // szerokosc ekranu
    int displayHeight = displaySize.getHeight(); // wysokosc ekranu
    int cameraHeight; // wspierana wysokosc kamery
    int cameraWidth; // wspierana szerokosc kamery

    // Lista przechowywujace SupportedPreviewSize kamery, wszyskie
    // rozdzielczosci mniejsze od szerokosc i wysokosci ekranu
    List<Point> lOptimalPoint = new ArrayList<Point>();

    // Pomocniczo do listowania zawartosci listy
    // TODO manta displayHeight cameraHeight brak oraz width brak zgodnosci
    // (
    System.out.println(lOptimalPoint.toString());
    for (int i = 0; i < lSuportedPreviewSize.size(); i++) {
        Log.i(TAG, "w " + lSuportedPreviewSize.get(i).width + " h " + lSuportedPreviewSize.get(i).height + " \n");
    }

    // Wyszukanie wszystkich wysokosci kamery mniejszej od wysokosci ekranu
    for (int i = 0; i < lSuportedPreviewSize.size(); i++) {
        // TODO Uwazaj kamera zapisuje swoj rozmiar dla pozycji landscape
        // gdzie height = 480 a width = 800
        cameraHeight = lSuportedPreviewSize.get(i).width;
        cameraWidth = lSuportedPreviewSize.get(i).height;
        // Porownaj wysokosc ekranu urzadzenia z wysokosci supportedPreview
        // dodaj do listy
        if (displayHeight > cameraHeight) {
            lOptimalPoint.add(new Point(cameraHeight, cameraWidth));
        }
    }

    // Sortowanie rosnaco
    Collections.sort(lOptimalPoint, new ComapreSupportedPreviewByWidth());
    // Ostatni element listy optymalny
    optimalPreviewSize = lOptimalPoint.get(lOptimalPoint.size()-1);

    // Zwracana rozdzielczosc landscape aparatu np (800x600)
    return optimalPreviewSize;
}

private void setDisplaySize(Display display) {
    Activity activity = (Activity) this.context; // Pobierz aktywnosc aby
                                                    // znać rozmiar ekranu

    this.display = activity.getWindowManager().getDefaultDisplay();
}

class ComapreSupportedPreviewByWidth implements Comparator<Point> {

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

}

链接到在 Eclipse 中进行的屏幕和项目

于 2015-07-21T14:41:37.107 回答
0

我已经解决了问题。什么可能导致奇怪的相机预览出现问题。

  • 状态栏需要空格 - 你可以隐藏它隐藏状态栏

  • 一些空间也采用 TitleBar - 您可以在清单中关闭它

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

  • 将活动方向更改为横向“因为相机预览支持该方向” - 您可以在API demo Graphics->CameraPreview中查看

  • 算法比较Display.getWidth()的大小,Camera.getParameters的大小()。getSupportedPreviewSizes (); 如果它们相同,是您在搜索列表时收到的 surfaceChanged 更改 Parametrs.setPreviewSize (x, y) 的函数


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<Point> lOptimalPoint = new ArrayList<Point>();

        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<Point> {

    @Override
    public int compare(Point lhs, Point rhs) {
        return lhs.x - rhs.x;
    }
于 2015-08-04T19:48:19.033 回答