Search in sources :

Example 1 with ReturnLisenter

use of com.cjt2325.cameralibrary.lisenter.ReturnLisenter in project CameraView by CJT2325.

the class CaptureLayout method initView.

private void initView() {
    setWillNotDraw(false);
    //btn_capture
    btn_capture = new CaptureButton(getContext(), button_size);
    LayoutParams btn_capture_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    btn_capture_param.addRule(CENTER_IN_PARENT, TRUE);
    btn_capture_param.setMargins(0, 152, 0, 0);
    btn_capture.setLayoutParams(btn_capture_param);
    btn_capture.setDuration(5000);
    btn_capture.setCaptureLisenter(new CaptureLisenter() {

        @Override
        public void takePictures() {
            if (captureLisenter != null) {
                captureLisenter.takePictures();
            }
        }

        @Override
        public void recordShort(long time) {
            if (captureLisenter != null) {
                captureLisenter.recordShort(time);
            }
            startAlphaAnimation();
        }

        @Override
        public void recordStart() {
            if (captureLisenter != null) {
                captureLisenter.recordStart();
            }
            startAlphaAnimation();
        }

        @Override
        public void recordEnd(long time) {
            if (captureLisenter != null) {
                captureLisenter.recordEnd(time);
            }
            startAlphaAnimation();
            startTypeBtnAnimator();
        }

        @Override
        public void recordZoom(float zoom) {
            if (captureLisenter != null) {
                captureLisenter.recordZoom(zoom);
            }
        }
    });
    //btn_cancel
    btn_cancel = new TypeButton(getContext(), TypeButton.TYPE_CANCEL, button_size);
    final LayoutParams btn_cancel_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    btn_cancel_param.addRule(CENTER_VERTICAL, TRUE);
    btn_cancel_param.addRule(ALIGN_PARENT_LEFT, TRUE);
    btn_cancel_param.setMargins((layout_width / 4) - button_size / 2, 0, 0, 0);
    btn_cancel.setLayoutParams(btn_cancel_param);
    btn_cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (typeLisenter != null) {
                typeLisenter.cancel();
            }
            startAlphaAnimation();
            btn_cancel.setVisibility(INVISIBLE);
            btn_confirm.setVisibility(INVISIBLE);
            btn_capture.setVisibility(VISIBLE);
            btn_return.setVisibility(VISIBLE);
        }
    });
    //btn_confirm
    btn_confirm = new TypeButton(getContext(), TypeButton.TYPE_CONFIRM, button_size);
    LayoutParams btn_confirm_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    btn_confirm_param.addRule(CENTER_VERTICAL, TRUE);
    btn_confirm_param.addRule(ALIGN_PARENT_RIGHT, TRUE);
    btn_confirm_param.setMargins(0, 0, (layout_width / 4) - button_size / 2, 0);
    btn_confirm.setLayoutParams(btn_confirm_param);
    btn_confirm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (typeLisenter != null) {
                typeLisenter.confirm();
            }
            startAlphaAnimation();
            btn_cancel.setVisibility(INVISIBLE);
            btn_confirm.setVisibility(INVISIBLE);
            btn_capture.setVisibility(VISIBLE);
            btn_return.setVisibility(VISIBLE);
        }
    });
    //btn_return
    btn_return = new ReturnButton(getContext(), button_size / 2);
    LayoutParams btn_return_param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    btn_return_param.addRule(CENTER_VERTICAL, TRUE);
    btn_return_param.setMargins(layout_width / 6, 0, 0, 0);
    btn_return.setLayoutParams(btn_return_param);
    btn_return.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (captureLisenter != null) {
                if (returnLisenter != null) {
                    returnLisenter.onReturn();
                }
            }
        }
    });
    //txt_tip
    txt_tip = new TextView(getContext());
    LayoutParams txt_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    txt_param.setMargins(0, 0, 0, 0);
    txt_tip.setText("轻触拍照,长按摄像");
    txt_tip.setTextColor(0xFFFFFFFF);
    txt_tip.setGravity(Gravity.CENTER);
    txt_tip.setLayoutParams(txt_param);
    this.addView(btn_capture);
    this.addView(btn_cancel);
    this.addView(btn_confirm);
    this.addView(btn_return);
    this.addView(txt_tip);
}
Also used : CaptureLisenter(com.cjt2325.cameralibrary.lisenter.CaptureLisenter) TextView(android.widget.TextView) TextView(android.widget.TextView) View(android.view.View)

Example 2 with ReturnLisenter

use of com.cjt2325.cameralibrary.lisenter.ReturnLisenter in project CameraView by CJT2325.

the class JCameraView method initView.

private void initView() {
    setWillNotDraw(false);
    this.setBackgroundColor(0xff000000);
    //VideoView
    mVideoView = new VideoView(mContext);
    LayoutParams videoViewParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    videoViewParam.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    mVideoView.setLayoutParams(videoViewParam);
    //mPhoto
    mPhoto = new ImageView(mContext);
    LayoutParams photoParam = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    photoParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    mPhoto.setLayoutParams(photoParam);
    mPhoto.setBackgroundColor(0xff000000);
    mPhoto.setVisibility(INVISIBLE);
    //switchCamera
    mSwitchCamera = new ImageView(mContext);
    LayoutParams imageViewParam = new LayoutParams(iconSize, iconSize);
    imageViewParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    imageViewParam.setMargins(0, iconMargin, iconMargin, 0);
    mSwitchCamera.setLayoutParams(imageViewParam);
    mSwitchCamera.setImageResource(iconSrc);
    mSwitchCamera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isBorrow || switching || forbiddenSwitch) {
                return;
            }
            switching = true;
            new Thread() {

                /**
                     * switch camera
                     */
                @Override
                public void run() {
                    CameraInterface.getInstance().switchCamera(JCameraView.this);
                }
            }.start();
        }
    });
    //CaptureLayout
    mCaptureLayout = new CaptureLayout(mContext);
    LayoutParams layout_param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layout_param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    layout_param.addRule(RelativeLayout.CENTER_HORIZONTAL);
    layout_param.setMargins(0, 0, 0, 40);
    mCaptureLayout.setLayoutParams(layout_param);
    mCaptureLayout.setDuration(duration);
    //mFoucsView
    mFoucsView = new FoucsView(mContext, fouce_size);
    LayoutParams foucs_param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    mFoucsView.setLayoutParams(foucs_param);
    mFoucsView.setVisibility(INVISIBLE);
    //add view to ParentLayout
    this.addView(mVideoView);
    this.addView(mPhoto);
    this.addView(mSwitchCamera);
    this.addView(mCaptureLayout);
    this.addView(mFoucsView);
    //START >>>>>>> captureLayout lisenter callback
    mCaptureLayout.setCaptureLisenter(new CaptureLisenter() {

        @Override
        public void takePictures() {
            if (CAMERA_STATE != STATE_IDLE || takePictureing) {
                return;
            }
            CAMERA_STATE = STATE_RUNNING;
            takePictureing = true;
            mFoucsView.setVisibility(INVISIBLE);
            CameraInterface.getInstance().takePicture(new CameraInterface.TakePictureCallback() {

                @Override
                public void captureResult(Bitmap bitmap) {
                    captureBitmap = bitmap;
                    CameraInterface.getInstance().doStopCamera();
                    type = TYPE_PICTURE;
                    isBorrow = true;
                    CAMERA_STATE = STATE_WAIT;
                    mPhoto.setImageBitmap(bitmap);
                    mPhoto.setVisibility(VISIBLE);
                    mCaptureLayout.startAlphaAnimation();
                    mCaptureLayout.startTypeBtnAnimator();
                    takePictureing = false;
                    mSwitchCamera.setVisibility(INVISIBLE);
                    CameraInterface.getInstance().doOpenCamera(JCameraView.this);
                }
            });
        }

        @Override
        public void recordShort(long time) {
            if (CAMERA_STATE != STATE_RUNNING && stopping) {
                return;
            }
            stopping = true;
            mCaptureLayout.setTextWithAnimation();
            postDelayed(new Runnable() {

                @Override
                public void run() {
                    CameraInterface.getInstance().stopRecord(true, new CameraInterface.StopRecordCallback() {

                        @Override
                        public void recordResult(String url) {
                            Log.i(TAG, "stopping ...");
                            mCaptureLayout.isRecord(false);
                            CAMERA_STATE = STATE_IDLE;
                            stopping = false;
                            isBorrow = false;
                        }
                    });
                }
            }, 1500 - time);
        }

        @Override
        public void recordStart() {
            if (CAMERA_STATE != STATE_IDLE && stopping) {
                return;
            }
            mCaptureLayout.isRecord(true);
            isBorrow = true;
            CAMERA_STATE = STATE_RUNNING;
            mFoucsView.setVisibility(INVISIBLE);
            CameraInterface.getInstance().startRecord(mVideoView.getHolder().getSurface(), new CameraInterface.ErrorCallback() {

                @Override
                public void onError() {
                    Log.i("CJT", "startRecorder error");
                    mCaptureLayout.isRecord(false);
                    CAMERA_STATE = STATE_WAIT;
                    stopping = false;
                    isBorrow = false;
                }
            });
        }

        @Override
        public void recordEnd(long time) {
            CameraInterface.getInstance().stopRecord(false, new CameraInterface.StopRecordCallback() {

                @Override
                public void recordResult(final String url) {
                    CAMERA_STATE = STATE_WAIT;
                    videoUrl = url;
                    type = TYPE_VIDEO;
                    new Thread(new Runnable() {

                        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                        @Override
                        public void run() {
                            try {
                                if (mMediaPlayer == null) {
                                    mMediaPlayer = new MediaPlayer();
                                } else {
                                    mMediaPlayer.reset();
                                }
                                Log.i("CJT", "URL = " + url);
                                mMediaPlayer.setDataSource(url);
                                mMediaPlayer.setSurface(mVideoView.getHolder().getSurface());
                                mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
                                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                                mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {

                                    @Override
                                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                                        updateVideoViewSize(mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight());
                                    }
                                });
                                mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                                    @Override
                                    public void onPrepared(MediaPlayer mp) {
                                        mMediaPlayer.start();
                                    }
                                });
                                mMediaPlayer.setLooping(true);
                                mMediaPlayer.prepare();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            });
        }

        @Override
        public void recordZoom(float zoom) {
            CameraInterface.getInstance().setZoom(zoom, CameraInterface.TYPE_RECORDER);
        }
    });
    mCaptureLayout.setTypeLisenter(new TypeLisenter() {

        @Override
        public void cancel() {
            if (CAMERA_STATE == STATE_WAIT) {
                if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                    mMediaPlayer.release();
                    mMediaPlayer = null;
                }
                //                    CameraInterface.getInstance().doOpenCamera(JCameraView.this);
                handlerPictureOrVideo(type, false);
            }
        }

        @Override
        public void confirm() {
            if (CAMERA_STATE == STATE_WAIT) {
                if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                    mMediaPlayer.release();
                    mMediaPlayer = null;
                }
                //                    CameraInterface.getInstance().doOpenCamera(JCameraView.this);
                handlerPictureOrVideo(type, true);
            }
        }
    });
    mCaptureLayout.setReturnLisenter(new ReturnLisenter() {

        @Override
        public void onReturn() {
            if (jCameraLisenter != null && !takePictureing) {
                jCameraLisenter.quit();
            }
        }
    });
    //END >>>>>>> captureLayout lisenter callback
    mVideoView.getHolder().addCallback(this);
}
Also used : CaptureLisenter(com.cjt2325.cameralibrary.lisenter.CaptureLisenter) VideoView(android.widget.VideoView) Bitmap(android.graphics.Bitmap) ReturnLisenter(com.cjt2325.cameralibrary.lisenter.ReturnLisenter) ImageView(android.widget.ImageView) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) VideoView(android.widget.VideoView) TypeLisenter(com.cjt2325.cameralibrary.lisenter.TypeLisenter) MediaPlayer(android.media.MediaPlayer)

Aggregations

View (android.view.View)2 CaptureLisenter (com.cjt2325.cameralibrary.lisenter.CaptureLisenter)2 Bitmap (android.graphics.Bitmap)1 MediaPlayer (android.media.MediaPlayer)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 VideoView (android.widget.VideoView)1 ReturnLisenter (com.cjt2325.cameralibrary.lisenter.ReturnLisenter)1 TypeLisenter (com.cjt2325.cameralibrary.lisenter.TypeLisenter)1 IOException (java.io.IOException)1