Skip to main content

Custom camera using SurfaceView android with autofocus & auto lights & more

Custom camera using SurfaceView android with autofocus & auto lights & much more

/**
 * @author Tatyabhau Chavan
 *
 */
public class Preview extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder mHolder;
    private Camera mCamera;
    public Camera.Parameters mParameters;
    private byte[] mBuffer;
    private Activity mActivity;

    // this constructor used when requested as an XML resource
    public Preview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Preview(Context context) {
        super(context);
        init();
    }

    public Camera getCamera() {
        return this.mCamera;
    }

    public void init() {
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public Bitmap getPic(int x, int y, int width, int height) {
        System.gc();
        Bitmap b = null;
        Size s = mParameters.getPreviewSize();

        YuvImage yuvimage = new YuvImage(mBuffer, ImageFormat.NV21, s.width,
                s.height, null);

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make
                                                                                // JPG

        b = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0,
                outStream.size()); // decode JPG
        Bitmap mBitmap = null;
        if (b != null) {
            Matrix mtrxFreeze = new Matrix();
            mtrxFreeze.postRotate(90);
            mBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(),
                    mtrxFreeze, true);
        } else {
        }
        yuvimage = null;
        outStream = null;
        System.gc();
        return b = mBitmap;
    }

    private void updateBufferSize() {
        mBuffer = null;
        System.gc();
        int h = mCamera.getParameters().getPreviewSize().height;
        int w = mCamera.getParameters().getPreviewSize().width;
        int bitsPerPixel = ImageFormat.getBitsPerPixel(mCamera.getParameters()
                .getPreviewFormat());
        mBuffer = new byte[w * h * bitsPerPixel / 8];
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera = Camera.open();
        } catch (RuntimeException exception) {
            Toast.makeText(getContext(), "Camera broken, quitting :(",
                    Toast.LENGTH_LONG).show();
            Logger.writeLOG(EnforcementConstants.LOG, "" + exception.toString());
        }

        try {
            mCamera.setPreviewDisplay(holder);
            updateBufferSize();
            mCamera.addCallbackBuffer(mBuffer); // where we'll store the image
                                                // data
            mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
                public synchronized void onPreviewFrame(byte[] data, Camera c) {
                    if (mCamera != null) {
                        // there was a race condition when
                        mCamera.addCallbackBuffer(mBuffer); // it was consumed
                    }
                }
            });
            mCamera.setDisplayOrientation(90);
        } catch (Exception exception) {
            Logger.writeLOG(EnforcementConstants.LOG, "" + exception.toString());
            if (mCamera != null) {
                mCamera.release();
                mCamera = null;
            }
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        // Log.i(TAG,"SurfaceDestroyed being called");
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    // FYI: not called for each frame of the camera preview
    // gets called on my phone when keyboard is slid out
    // requesting landscape orientation prevents this from being called as
    // camera tilts

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

        try {
            setParameters(mCamera);
        } catch (Exception e) {

            Logger.writeLOG(EnforcementConstants.LOG, "" + e.toString());

        }
        updateBufferSize(); // then use them to calculate
        mCamera.startPreview();
    }

    /**
     *
     */
    public void setParameters(Camera mCamera) {
        mParameters = mCamera.getParameters();
        mParameters.set("orientation", "portrait");

        mParameters.setPreviewSize(
                mCamera.getParameters().getPreviewSize().width, mCamera
                        .getParameters().getPreviewSize().height);
        List<?> focus = mParameters.getSupportedFocusModes();
        if (focus != null
                && focus.contains(android.hardware.Camera.Parameters.FOCUS_MODE_AUTO)) {
            mParameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
        }

        List<?> whiteMode = mParameters.getSupportedWhiteBalance();
        if (whiteMode != null
                && whiteMode
                        .contains(android.hardware.Camera.Parameters.WHITE_BALANCE_AUTO)) {
            mParameters.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
        }

        List<String> flashModes = mParameters.getSupportedFlashModes();

        if (flashModes
                .contains(android.hardware.Camera.Parameters.FLASH_MODE_AUTO)) {
            mParameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
        }

        mCamera.setParameters(mParameters); // apply the changes
    }

    public Parameters getCameraParameters() {
        return mCamera.getParameters();
    }

    public void setFlash(boolean flash) {
        if (flash) {
            mParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(mParameters);
        } else {
            mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
            mCamera.setParameters(mParameters);
        }
    }

    /**
     * @return the mActivity
     */

    public Activity getActivity() {
        return mActivity;
    }

    /**
     * @param mActivity
     *            the mActivity to set
     */
    public void setActivity(Activity mActivity) {
        this.mActivity = mActivity;
    }
}

Comments

Popular posts from this blog

Volley for client and server interaction (Android)

Volley provides client and server interaction with QUEUE basis, each and every request process separately, we can cancel our request from QUEUE. HERE is a code sample for Volley with json request format. GenerateRequest.java -------------------------------------------------------------------- package com.entrata.maintenance.network_communication.networkutils; import android.content.Context; import com.android.volley.AuthFailureError; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.entrata.maintenance.application.ERPUserDefaults; import com.entrata.maintenance.network_communication.networkconstant.NetworkConstant; import com.entrata.maintenance.network_communication.networkexception.Error; import com.entrata.maintenance.network_communication.networkutil.SubdomainChange; import com.entrata.maintenance.utils.Logger; import com.google.gson.Gson; import org.json.JSONException; import org.js...
Java Code Examples for javax.net.ssl.HttpsURLConnection The following code examples are extracted from open source projects. You can click to vote up the examples you like. Your votes will be used in an intelligent system to get more and better code examples. Thanks for your votes! Code Example 1:   9  From project ADFS , under directory /adfs-hdfs-project/adfs-hdfs/src/main/java/org/apache/hadoop/hdfs/ . Source HsftpFileSystem.java @Override protected HttpURLConnection openConnection ( String path , String query ) throws IOException { query = addDelegationTokenParam ( query ); final URL url = new URL ( "https" , nnAddr . getHostName (), nnAddr . getPort (), path + '?' + query ); HttpsURLConnection conn =( HttpsURLConnection ) url . openConnection (); conn . setHostnameVerifier ( new DummyHostnameVerifier ()); return ( HttpURLConnection ) conn ; } Code Example 2:  ...

Improving Layout Performance

Improving Layout Performance Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint. Lessons Optimizing Layout Hierarchies In the same way a complex web page can slow down load time, your layout hierarchy if too complex can also cause performance problems. This lesson shows how you can use SDK tools to inspect your layout and discover performance bottlenecks. Re-using Layouts with <include/> If your application UI repeats certain layout constructs in multiple places, this lesson shows you how to create efficient, re-usable layout constructs, then include them in the appropriate UI layouts. Loading Views On Demand Be...