Android Open Source - Cam2IMU_calib Camera Preview






From Project

Back to project page Cam2IMU_calib.

License

The source code is released under:

MIT License

If you think the Android project Cam2IMU_calib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cvg.vslam.c2i_calib;
//  w w w.j  a  va 2s .c  o m
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.hardware.Camera;
import android.view.View;

import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: fede
 * Date: 7/17/13
 * Time: 1:59 PM
 * To change this template use File | Settings | File Templates.
 */

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "CameraPreview";
    private static final int Height = 480;
    private static final int Width = 640;
    private static final double ASPECT_RATIO = 3.0 / 4.0;


    private int mFrameWidth;
    private int mFrameHeight;

    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview (Context context){
        super(context);
        // Setup callbacks for the holder
        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    public CameraPreview (Context context, AttributeSet attributeSet){
        super(context, attributeSet);
        // Setup callbacks for the holder
        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    public CameraPreview (Context context, AttributeSet attributeSet, int style){
        super(context, attributeSet, style);
        // Setup callbacks for the holder
        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    public void init(Camera camera){
        mCamera = camera;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder){
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
        if(mHolder.getSurface() == null){
            return;
        }

        try {
            mCamera.stopPreview();
        } catch(Exception e) {

        }

        try {
            Camera.Parameters params = mCamera.getParameters();
            List<Camera.Size> sizes = params.getSupportedPreviewSizes();

            Log.i(TAG, "Selecting video resolution from:");
            // selecting optimal camera preview size
            int minDiffW = Integer.MAX_VALUE;
            int minDiffH = Integer.MAX_VALUE;
            for (Camera.Size size : sizes)
            {
                Log.i(TAG, " " + size.width + "x" + size.height);
                if (size.height - Height <= minDiffH && size.height - Height >= 0 &&
                        size.width - Width <= minDiffW && size.width - Width >= 0)
                {
                    mFrameWidth = size.width;
                    mFrameHeight = size.height;
                    minDiffH = size.height - Height;
                    minDiffW = size.width - Width;
                }
            }
            Log.e(TAG, "Selecting size to "+ mFrameWidth + "x" + mFrameHeight);
            mCamera.setDisplayOrientation(90);
            params.setPreviewSize(mFrameHeight, mFrameWidth);
            params.setPictureSize(mFrameWidth, mFrameHeight);
            params.setFocusMode(params.FOCUS_MODE_INFINITY);
            requestLayout();
            mCamera.setParameters(params);


            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e) {
            Log.d(TAG, "Error starting preview: " + e.getMessage());
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        // Portrait dimensions but app built to be used horizontally ...
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = (int) (width / ASPECT_RATIO + .5);
        setMeasuredDimension(width, height);
    }

}




Java Source Code List

cvg.vslam.c2i_calib.CameraPreview.java
cvg.vslam.c2i_calib.MainActivity.java
cvg.vslam.c2i_calib.SaveDataDialog.java
cvg.vslam.c2i_calib.SensorBuffer.java