Android Custom Camera(2)

Android camera flash mode, square shot, delayed shooting and front and back camera switching function

Through the introduction of an Android custom Camera (一), we have been able to complete a camera based on the demo, of course, there are a lot of attention to the details of the problem, such as the preview direction, set the preview ratio and return the proportion of pictures and so on , The details can see previous blog, this time I will enrich the camera’s function to join the flash mode, square shooting, delay shooting and front and rear camera switching function, then one by one to start it. (There will be a complete source address below).

Flash mode

The camera’s flash mode there are three, automatic, close, open, the code is also better understand, before the camera can switch calls. Note I added a note in code below:

/**
     * turnLightOff
     *
     * @param mCamera
     */
    public void turnLightOff(Camera mCamera) {
        if (mCamera == null) {
            return;
        }
        Camera.Parameters parameters = mCamera.getParameters();
        if (parameters == null) {
            return;
        }
        List<String> flashModes = parameters.getSupportedFlashModes();
        String flashMode = parameters.getFlashMode();
        // Check if camera flash exists
        if (flashModes == null) {
            return;
        }
        if (!Camera.Parameters.FLASH_MODE_OFF.equals(flashMode)) {
            // Turn off the flash
            if (flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                mCamera.setParameters(parameters);
            } else {
            }
        }
    }

2 automatic mode:

/**
     * turnLightAuto
     *
     * @param mCamera
     */
    public void turnLightAuto(Camera mCamera) {
        if (mCamera == null) {
            return;
        }
        Camera.Parameters parameters = mCamera.getParameters();
        if (parameters == null) {
            return;
        }
        List<String> flashModes = parameters.getSupportedFlashModes();
        // Check if camera flash exists
        if (flashModes == null) {
            // Use the screen as a flashlight (next best thing)
            return;
        }
        String flashMode = parameters.getFlashMode();
        if (!Camera.Parameters.FLASH_MODE_AUTO.equals(flashMode)) {
            // Turn on the flash
            if (flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                mCamera.setParameters(parameters);
            } else {
            }
        }
    }

3 open

 @param mCamera
     */
    public void turnLightOn(Camera mCamera) {
        if (mCamera == null) {
            return;
        }
        Camera.Parameters parameters = mCamera.getParameters();
        if (parameters == null) {
            return;
        }
        List<String> flashModes = parameters.getSupportedFlashModes();
        // Check if camera flash exists
        if (flashModes == null) {
            // Use the screen as a flashlight (next best thing)
            return;
        }
        String flashMode = parameters.getFlashMode();
        if (!Camera.Parameters.FLASH_MODE_ON.equals(flashMode)) {
            // Turn on the flash
            if (flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                mCamera.setParameters(parameters);
            } else {
            }
        }
    }

Square shot

Here I want to talk about how I do, at present I think because we look at the proportion of framing frames must meet the size of mobile phone support, there are surfaceView and preview the proportion of the same size, so that we can not determine the size of the frame is specific How many, so I achieved a square shot with the way to cover the viewfinder frame, shooting time frame will be covered into a square, in the back of the picture and then cut it into a square can be, in the current project I also In this way, because many mobile phones are directly supporting the square size, where I used two attribute animation to cover the viewfinder, the code is as follows, here is to calculate the height to cover.

/**
     * camera_square
     */
    public void camera_square_0() {
        camera_square.setImageResource(R.drawable.btn_camera_size1_n);

        ValueAnimator anim = ValueAnimator.ofInt(0, animHeight);
        anim.setDuration(300);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentValue = Integer.parseInt(animation.getAnimatedValue().toString());
                RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(screenWidth, currentValue);
                Params.setMargins(0, SystemUtils.dp2px(context, 44), 0, 0);
                homeCustom_cover_top_view.setLayoutParams(Params);

                RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(screenWidth, currentValue);
                bottomParams.setMargins(0, screenHeight - menuPopviewHeight - currentValue, 0, 0);
                homeCustom_cover_bottom_view.setLayoutParams(bottomParams);
            }

        });
        anim.start();

        homeCustom_cover_top_view.bringToFront();
        home_custom_top_relative.bringToFront();
        homeCustom_cover_bottom_view.bringToFront();
        index++;
    }

    public void camera_square_1() {
        camera_square.setImageResource(R.drawable.btn_camera_size2_n);

        ValueAnimator anim = ValueAnimator.ofInt(animHeight, 0);
        anim.setDuration(300);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentValue = Integer.parseInt(animation.getAnimatedValue().toString());
                RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(screenWidth, currentValue);
                Params.setMargins(0, SystemUtils.dp2px(context, 44), 0, 0);
                homeCustom_cover_top_view.setLayoutParams(Params);

                RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(screenWidth, currentValue);
                bottomParams.setMargins(0, screenHeight - menuPopviewHeight - currentValue, 0, 0);
                homeCustom_cover_bottom_view.setLayoutParams(bottomParams);
            }
        });
        anim.start();
        index = 0;
    }

Delayed shooting

First of all, say the principle of delay shooting, in fact, more than one way to achieve, where I use the thread plus the way the main code is as follows, first recorded a delay time, and then decrement this time every 1s, and finally it is equal to 0 When the implementation of the shooting code, the effect I will not paste it, you can download the code to run to see, that is, a large number of seconds on the screen

new Thread(new Runnable() {
                            @Override
                            public void run() {
                                while (delay_time > 0) {
                                    //Count in seconds
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        mHandler.sendEmptyMessage(AppConstant.WHAT.ERROR);
                                        return;
                                    }
                                    delay_time--;
                                    mHandler.sendEmptyMessage(AppConstant.WHAT.SUCCESS);
                                }
                            }
                        }).start();

Switch the front rear camera

Almost all of the camera program needs this function, must be able to front and rear ah, that in fact, this is also relatively simple to achieve, when the camera is in the process of previewing before and after the switch must first release the camera resources, and then And then open the preview, switch the camera is nothing more than switch cameraId, here look at the next code:

 public void  switchCamera() {
        releaseCamera();
        //This method getNumberOfCameras () is introduced from the API Level 9, get the number of cameras in the normal normal mobile phone only one camera, here I also follow a camera to deal with the
        mCameraId = (mCameraId + 1) % mCamera.getNumberOfCameras();
        mCamera = getCamera(mCameraId);
        if (mHolder != null) {
            startPreview(mCamera, mHolder);
        }
    }

Add watermark

The way to say that the function of the watermark it, plus watermark function is not difficult to achieve, it is rare to draw a good watermark, in fact, watermark is also in the camera to return to the bitmap to join, in fact, is painted with Canvas class, the core The code is as follows, assuming the camera returns the picture shown as bitmap:

Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(tempbitmap, left, top, paint);

Code is generally the case, through the canvas.drawBitmap method to draw up, of course, I am free to draw the position. But you record the location of the watermark when previewing, and then painted up properly.

The following is the source address:

https://github.com/jinguangyue/CustomCamera

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: