Android Open Source - AndroidWifiServer Camera Surface View






From Project

Back to project page AndroidWifiServer.

License

The source code is released under:

Apache License

If you think the Android project AndroidWifiServer 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 jp.maju.wifiserver.camera;
/*from   ww w  . ja v a2s .c  om*/
import java.io.IOException;
import java.util.List;

import jp.maju.wifiserver.util.Logger;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.PreviewCallback;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class CameraSurfaceView extends SurfaceView {
    private static final String TAG = CameraSurfaceView.class.getSimpleName();
    private Camera mCamera;
    private HolderCallback mHolderCallback;
    private SeekBar mSeekBar;

    public CameraSurfaceView(Context context) {
        super(context);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs, int resId) {
        super(context, attrs, resId);
    }

    public CameraSurfaceView initialize(SeekBar zoomSeek) {
        mSeekBar = zoomSeek;
        mHolderCallback = new HolderCallback();
        getHolder().addCallback(mHolderCallback);
        mSeekBar.setOnSeekBarChangeListener(mHolderCallback);
        return this;
    }

    public void toggleChangeCamera() {
        mHolderCallback.toggleChangeCamera();
    }

    public void zoomUp() {
        int currentProgress = mSeekBar.getProgress();

        if (currentProgress != mSeekBar.getMax()) {
            mSeekBar.setProgress(mSeekBar.getProgress() + 1);
        }
    }

    public void zoomDown() {
        int currentProgress = mSeekBar.getProgress();

        if (currentProgress != 0) {
            mSeekBar.setProgress(mSeekBar.getProgress() - 1);
        }
    }

    private void setCameraOrientation(Camera.CameraInfo info, int rotation) {
        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;
        }

        mCamera.setDisplayOrientation(result);
    }

    public void shutter(final PictureCallback mPictureCallback,
            final int retryCount) {
        mCamera.autoFocus(new AutoFocusCallback() {

            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                if (success) {
                    mCamera.takePicture(null, null, mPictureCallback);
                } else if (retryCount > 3) {
                    return;
                } else {
                    shutter(mPictureCallback, retryCount + 1);
                }
            }
        });
    }

    public synchronized void oneShot(final PreviewCallback previewCallback,
            final int retryCount) {
        if (getTag() != null) {
            boolean isAutofocusing = (boolean) getTag();
            if (isAutofocusing) {
                return;
            }
        }
        
        setTag(true);
        mCamera.autoFocus(new AutoFocusCallback() {

            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                if (success) {
                    mCamera.setOneShotPreviewCallback(previewCallback);
                    setTag(false);
                } else if (retryCount > 4) {
                    setTag(false);
                } else {
                    setTag(false);
                    oneShot(previewCallback, retryCount + 1);
                }
            }
        });
    }

    private class HolderCallback implements SurfaceHolder.Callback,
            OnSeekBarChangeListener {
        private int cameraId;
        private Camera.CameraInfo caminfo;
        private int facing = Camera.CameraInfo.CAMERA_FACING_BACK;
        private int maxZoomLevel;

        public void toggleChangeCamera() {
            switch (facing) {
                case Camera.CameraInfo.CAMERA_FACING_BACK:
                    facing = Camera.CameraInfo.CAMERA_FACING_FRONT;
                    break;
                case Camera.CameraInfo.CAMERA_FACING_FRONT:
                    facing = Camera.CameraInfo.CAMERA_FACING_BACK;
                    break;
                default:
                    // ???????????
            }

            surfaceDestroyed(getHolder());
            surfaceCreated(getHolder());

            startPreview();
        }

        @SuppressLint("NewApi")
        private void startPreview() {
            if (getContext() instanceof Activity) {
                int rotation = ((Activity) getContext()).getWindowManager()
                        .getDefaultDisplay().getRotation();

                setCameraOrientation(caminfo, rotation);
            } else {
                Logger.d(
                        TAG,
                        "Context is not a instance of Activity. Orientation of Camera may be incorrect.");
            }

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                mCamera.enableShutterSound(false);
            } else {

            }
            mCamera.startPreview();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            Camera.Parameters parameters = mCamera.getParameters();
            List<Camera.Size> previewSizes = parameters
                    .getSupportedPreviewSizes();
            Camera.Size previewSize = previewSizes.get(0);
            for (Camera.Size s : previewSizes) {
                if (s.width > previewSize.width) {
                    previewSize = s;
                }
            }

            parameters.setPreviewSize(previewSize.width, previewSize.height);
            parameters.setPictureSize(previewSize.width, previewSize.height);
            mCamera.setParameters(parameters);

            startPreview();
        }

        @SuppressLint("NewApi")
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
                caminfo = new Camera.CameraInfo();
                Camera.getCameraInfo(i, caminfo);

                // ?????????????????
                int facing = caminfo.facing;

                if (facing == this.facing) {
                    mCamera = Camera.open(i);
                    cameraId = i;
                    break;
                }
            }

            Camera.Parameters parameters = mCamera.getParameters();
            maxZoomLevel = parameters.getMaxZoom();
            mSeekBar.setProgress(0);
            mSeekBar.setMax(maxZoomLevel);
            parameters.setJpegQuality(100);
            if (Build.VERSION_CODES.ICE_CREAM_SANDWICH < Build.VERSION.SDK_INT) {
                parameters.setVideoStabilization(true);
            }

            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                Logger.e(TAG, "IOException", e);
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if (seekBar.getId() == mSeekBar.getId() && maxZoomLevel != 0) {
                mCamera.startSmoothZoom(progress);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    }
}




Java Source Code List

jp.maju.wifiserver.AsyncExecutionTask.java
jp.maju.wifiserver.CustomWebView.java
jp.maju.wifiserver.DBAdapter.java
jp.maju.wifiserver.GateActivity.java
jp.maju.wifiserver.HTMLBuilder.java
jp.maju.wifiserver.SocketInfo.java
jp.maju.wifiserver.camera.CameraSurfaceView.java
jp.maju.wifiserver.camera.QRReaderActivity.java
jp.maju.wifiserver.client.ClientActivity.java
jp.maju.wifiserver.client.ClientService.java
jp.maju.wifiserver.server.InitServerActivity.java
jp.maju.wifiserver.server.ServerActivity.java
jp.maju.wifiserver.server.ServerService.java
jp.maju.wifiserver.twitter.ProxyDialogFragment.java
jp.maju.wifiserver.twitter.ProxyWrapper.java
jp.maju.wifiserver.twitter.TweetTask.java
jp.maju.wifiserver.twitter.TwitterOAuthActivity.java
jp.maju.wifiserver.twitter.TwitterUtils.java
jp.maju.wifiserver.util.CommonUtil.java
jp.maju.wifiserver.util.Logger.java
jp.maju.wifiserver.util.PreferenceUtil.java