Some Functions in Android Camera Development

Keywords: Android

Recently, a graphics processing program based on Android camera has been studied. In order to control photography, because of the single function of the camera, it is necessary to add sound to remind users of the completion of the photographic action. At the same time, it is necessary to set whether the flash is turned on or not, so as to achieve the purpose of denoising in different environments as far as possible. Because two Buttens are set up in this program, two Buttens need to be distinguished, because clicking on different Buttens corresponds to different functions.

I. Addition of Shutter Sound

  private Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
        public void onShutter() {
            tone = new ToneGenerator(AudioManager.STREAM_MUSIC, ToneGenerator.MIN_VOLUME);
        }
    };
camera.shutterCallback method is called. This method is used in camera.takePicture method. It can be seen from the parameter setting of camera.takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg). The first parameter is the callback after capturing the image. At this time, sound can be set to indicate that the user's image has been captured. . The second parameter is to call back the original image data, which can be null. The third parameter is the method of image callback processing.

2. Realization of Flash Opening Control

public void flashSelection() {
        flash_on = (ImageButton) findViewById(R.id.falsh_on);
        flash_on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != camera) {
                    Camera.Parameters parameters = camera.getParameters();
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                }
            }
        });
        flash_off = (ImageButton) findViewById(R.id.falsh_off);
        flash_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Camera.Parameters parameters = camera.getParameters();
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                camera.setParameters(parameters);
            }
        });
    }
Two Buttens are set up here to control the turn-on and turn-off of the flash. This method is very simple. Because I have just started contacting, I do not know how to set up a Butten, and then through selector control, tried many times, all failed, I hope God gives a direction.

3. Realization of Different Butten s Corresponding to Different Trigger Events

public void onClick(final View v) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                switch (v.getId()) {
                    case R.id.take:
                        camera.takePicture(shutterCallback,null,new MyPictureCallback());
                        System.out.println(">++++++++<");
                        break;
                    case R.id.take1:
                        camera.takePicture(shutterCallback,null,new SecondPictureCallback());
                        System.out.println(">?????????<");
                        Message message = new Message();
                        message.what = res;
                        handler.sendMessage(message);
                        break;
                    default:
                        break;
                }
            }
        });
        thread.start();
    }
private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//Set Underlines
                    tv.setText(Html.fromHtml(" " + res + ""));
                    break;
                default:
                    break;
            }
        }
    };

Here a thread is called to run some time-consuming operations. When dealing with two or more Buttens, it should be noted that two or more Buttens'IDs are used to judge, and View in onClick function is used to judge different id s, so as to realize different triggers.


Posted by happyme on Fri, 05 Apr 2019 09:24:30 -0700