안 드 로 이 드 최신 위 챗 카메라 기능 모방
대체적으로 완 성 된 기능 은 다음 과 같다.
4.567917.촬영 영상 을 길 게 누 르 고 가볍게 사진 을 찍 습 니 다.
앞 뒤 카메라 전환
4.567917.플래시 의 오픈,닫 기,자동그림 의 압축
자동 초점,수 동 초점
효과 도 는 다음 과 같다.
관련 코드 는 다음 과 같 습 니 다.
package com.ses.im.app.chat.newcamera;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ses.im.app.chat.R;
import java.io.File;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
*
*
*/
public class CameraActivity extends AppCompatActivity implements View.OnClickListener {
/**
*
*/
public static final int REQUEST_PHOTO = 1;
/**
*
*/
public static final int REQUEST_VIDEO = 2;
/**
*
*/
private static final int MIN_RECORD_TIME = 1 * 1000;
/**
*
*/
private static final int MAX_RECORD_TIME = 15 * 1000;
/**
*
*/
private static final int PLUSH_PROGRESS = 100;
private Context mContext;
/**
* TextureView
*/
private TextureView mTextureView;
/**
*
*/
private CameraView mCameraView;
/**
*
*/
private CameraProgressBar mProgressbar;
/**
*
*/
private RelativeLayout rl_camera;
/**
* , ,
*/
private ImageView iv_close, iv_facing;
private RelativeLayout iv_choice;
private RelativeLayout cancel;
/**
*
*/
private TextView tv_flash;
/**
* camera manager
*/
private CameraManager cameraManager;
/**
* player manager
*/
private MediaPlayerManager playerManager;
/**
* true ,
*/
private boolean isSupportRecord;
/**
*
*/
private String recorderPath;
/**
*
*/
private String photoPath;
/**
* ,
*/
private int recordSecond;
/**
* ,
*/
private Subscription takePhotoSubscription, progressSubscription;
/**
*
*/
private boolean isRecording;
/**
* ( )
*/
private boolean isPhotoTakingState;
public static void lanuchForPhoto(Activity context) {
Intent intent = new Intent(context, CameraActivity.class);
context.startActivityForResult(intent, REQUEST_PHOTO);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_camera);
initView();
initDatas();
}
private void initView() {
mTextureView = (TextureView) findViewById(R.id.mTextureView);
mCameraView = (CameraView) findViewById(R.id.mCameraView);
mProgressbar = (CameraProgressBar) findViewById(R.id.mProgressbar);
rl_camera = (RelativeLayout) findViewById(R.id.rl_camera);
iv_close = (ImageView) findViewById(R.id.iv_close);
iv_close.setOnClickListener(this);
iv_choice = (RelativeLayout) findViewById(R.id.iv_choice);
iv_choice.setOnClickListener(this);
iv_close.setOnClickListener(this);
iv_facing = (ImageView) findViewById(R.id.iv_facing);
iv_facing.setOnClickListener(this);
iv_close.setOnClickListener(this);
tv_flash = (TextView) findViewById(R.id.tv_flash);
tv_flash.setOnClickListener(this);
cancel= (RelativeLayout) findViewById(R.id.cancel);
cancel.setOnClickListener(this);
}
protected void initDatas() {
cameraManager = CameraManager.getInstance(getApplication());
playerManager = MediaPlayerManager.getInstance(getApplication());
cameraManager.setCameraType(isSupportRecord ? 1 : 0);
tv_flash.setVisibility(cameraManager.isSupportFlashCamera() ? View.VISIBLE : View.GONE);
setCameraFlashState();
iv_facing.setVisibility(cameraManager.isSupportFrontCamera() ? View.VISIBLE : View.GONE);
rl_camera.setVisibility(cameraManager.isSupportFlashCamera()
|| cameraManager.isSupportFrontCamera() ? View.VISIBLE : View.GONE);
final int max = MAX_RECORD_TIME / PLUSH_PROGRESS;
mProgressbar.setMaxProgress(max);
/**
* ,
*/
mProgressbar.setOnProgressTouchListener(new CameraProgressBar.OnProgressTouchListener() {
@Override
public void onClick(CameraProgressBar progressBar) {
cameraManager.takePhoto(callback);
isSupportRecord = false;
}
@Override
public void onLongClick(CameraProgressBar progressBar) {
isSupportRecord = true;
cameraManager.setCameraType(1);
rl_camera.setVisibility(View.GONE);
recorderPath = FileUtils.getUploadVideoFile(mContext);
cameraManager.startMediaRecord1(recorderPath);
isRecording = true;
progressSubscription = Observable.interval(100, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()).take(max).subscribe(new Subscriber<Long>() {
@Override
public void onCompleted() {
stopRecorder(true);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Long aLong) {
mProgressbar.setProgress(mProgressbar.getProgress() + 1);
}
});
}
@Override
public void onZoom(boolean zoom) {
cameraManager.handleZoom(zoom);
}
@Override
public void onLongClickUp(CameraProgressBar progressBar) {
// isSupportRecord = false;
cameraManager.setCameraType(0);
stopRecorder(true);
if (progressSubscription != null) {
progressSubscription.unsubscribe();
}
}
@Override
public void onPointerDown(float rawX, float rawY) {
if (mTextureView != null) {
mCameraView.setFoucsPoint(new PointF(rawX, rawY));
}
}
});
/**
*
*/
mCameraView.setOnViewTouchListener(new CameraView.OnViewTouchListener() {
@Override
public void handleFocus(float x, float y) {
cameraManager.handleFocusMetering(x, y);
}
@Override
public void handleZoom(boolean zoom) {
cameraManager.handleZoom(zoom);
}
});
}
/**
*
*/
private void setCameraFlashState() {
int flashState = cameraManager.getCameraFlash();
switch (flashState) {
case 0: //
tv_flash.setSelected(true);
tv_flash.setText(" ");
break;
case 1://open
tv_flash.setSelected(true);
tv_flash.setText(" ");
break;
case 2: //close
tv_flash.setSelected(false);
tv_flash.setText(" ");
break;
}
}
/**
*
* @param isShow
*/
private void setTakeButtonShow(boolean isShow) {
if (isShow) {
mProgressbar.setVisibility(View.VISIBLE);
rl_camera.setVisibility(cameraManager.isSupportFlashCamera()
|| cameraManager.isSupportFrontCamera() ? View.VISIBLE : View.GONE);
} else {
mProgressbar.setVisibility(View.GONE);
rl_camera.setVisibility(View.GONE);
}
}
/**
*
*/
private void stopRecorder(boolean play) {
isRecording = false;
cameraManager.stopMediaRecord();
recordSecond = mProgressbar.getProgress() * PLUSH_PROGRESS;//
mProgressbar.reset();
if (recordSecond < MIN_RECORD_TIME) {//
if (recorderPath != null) {
FileUtils.delteFiles(new File(recorderPath));
recorderPath = null;
recordSecond = 0;
}
setTakeButtonShow(true);
} else if (play && mTextureView != null && mTextureView.isAvailable()){
setTakeButtonShow(false);
mProgressbar.setVisibility(View.GONE);
iv_choice.setVisibility(View.VISIBLE);
cancel.setVisibility(View.VISIBLE);
iv_close.setVisibility(View.GONE);
cameraManager.closeCamera();
playerManager.playMedia(new Surface(mTextureView.getSurfaceTexture()), recorderPath);
}
}
@Override
protected void onResume() {
super.onResume();
if (mTextureView.isAvailable()) {
if (recorderPath != null) {//
iv_choice.setVisibility(View.VISIBLE);
setTakeButtonShow(false);
playerManager.playMedia(new Surface(mTextureView.getSurfaceTexture()), recorderPath);
} else {
iv_choice.setVisibility(View.GONE);
setTakeButtonShow(true);
cameraManager.openCamera(mTextureView.getSurfaceTexture(),
mTextureView.getWidth(), mTextureView.getHeight());
}
} else {
mTextureView.setSurfaceTextureListener(listener);
}
}
@Override
protected void onPause() {
if (progressSubscription != null) {
progressSubscription.unsubscribe();
}
if (takePhotoSubscription != null) {
takePhotoSubscription.unsubscribe();
}
if (isRecording) {
stopRecorder(false);
}
cameraManager.closeCamera();
playerManager.stopMedia();
super.onPause();
}
@Override
protected void onDestroy() {
mCameraView.removeOnZoomListener();
super.onDestroy();
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.iv_close) {
// if (recorderPath != null) {// ,
// FileUtils.delteFiles(new File(recorderPath));
// recorderPath = null;
// recordSecond = 0;
// playerManager.stopMedia();
// setTakeButtonShow(true);
// iv_choice.setVisibility(View.GONE);
// cameraManager.openCamera(mTextureView.getSurfaceTexture(), mTextureView.getWidth(), mTextureView.getHeight());
// } else if (isPhotoTakingState) {
// isPhotoTakingState = false;
// iv_choice.setVisibility(View.GONE);
// setTakeButtonShow(true);
// cameraManager.restartPreview();
// } else {
finish();
// }
} else if (i == R.id.iv_choice) {//
Intent intent=new Intent();
if(isSupportRecord){
intent.putExtra("videopath", recorderPath);
setResult(3, intent);
finish();
}else{
intent.putExtra("videopath", photoPath);
setResult(3, intent);
finish();
}
} else if (i == R.id.tv_flash) {
cameraManager.changeCameraFlash(mTextureView.getSurfaceTexture(),
mTextureView.getWidth(), mTextureView.getHeight());
setCameraFlashState();
} else if (i == R.id.iv_facing) {
cameraManager.changeCameraFacing(mTextureView.getSurfaceTexture(),
mTextureView.getWidth(), mTextureView.getHeight());
}else if(i == R.id.cancel){
if (recorderPath != null) {// ,
FileUtils.delteFiles(new File(recorderPath));
recorderPath = null;
recordSecond = 0;
playerManager.stopMedia();
setTakeButtonShow(true);
iv_choice.setVisibility(View.GONE);
cancel.setVisibility(View.GONE);
iv_close.setVisibility(View.VISIBLE);
cameraManager.openCamera(mTextureView.getSurfaceTexture(), mTextureView.getWidth(), mTextureView.getHeight());
} else if (isPhotoTakingState) {
isPhotoTakingState = false;
iv_choice.setVisibility(View.GONE);
cancel.setVisibility(View.GONE);
iv_close.setVisibility(View.VISIBLE);
setTakeButtonShow(true);
cameraManager.restartPreview();
}
}
}
/**
* camera
*/
private TextureView.SurfaceTextureListener listener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
if (recorderPath != null) {
iv_choice.setVisibility(View.VISIBLE);
setTakeButtonShow(false);
playerManager.playMedia(new Surface(texture), recorderPath);
} else {
setTakeButtonShow(true);
iv_choice.setVisibility(View.GONE);
cameraManager.openCamera(texture, width, height);
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture texture) {
}
};
private Camera.PictureCallback callback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
setTakeButtonShow(false);
takePhotoSubscription = Observable.create(new Observable.OnSubscribe<Boolean>() {
@Override
public void call(Subscriber<? super Boolean> subscriber) {
if (!subscriber.isUnsubscribed()) {
photoPath = FileUtils.getUploadPhotoFile(mContext);
isPhotoTakingState = FileUtils.savePhoto(photoPath, data, cameraManager.isCameraFrontFacing());
subscriber.onNext(isPhotoTakingState);
}
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Boolean aBoolean) {
if (aBoolean != null && aBoolean) {
iv_choice.setVisibility(View.VISIBLE);
cancel.setVisibility(View.VISIBLE);
iv_close.setVisibility(View.GONE);
} else {
setTakeButtonShow(true);
}
}
});
}
};
}
카메라 관리 클래스:
package com.ses.im.app.chat.newcamera;
import android.app.Application;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.util.Log;
import android.widget.Toast;
import com.ses.im.app.chat.util.AppConfig;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
*
*/
public final class CameraManager {
private Application context;
/**
* camera
*/
private Camera mCamera;
private Camera.Parameters mParameters;
/**
*
*/
private MediaRecorder mMediaRecorder;
/**
*
*/
private int cameraFlash;
/**
*
*/
private int cameraFacing = Camera.CameraInfo.CAMERA_FACING_BACK;
/**
* ,
*/
private boolean isSupportFrontCamera, isSupportFlashCamera;
/**
*
*/
private CamcorderProfile mProfile;
/**
* 0 , 1
*/
private int cameraType;
private CameraManager(Application context) {
this.context = context;
isSupportFrontCamera = CameraUtils.isSupportFrontCamera();
isSupportFlashCamera = CameraUtils.isSupportFlashCamera(context);
if (isSupportFrontCamera) {
cameraFacing = CameraUtils.getCameraFacing(context, Camera.CameraInfo.CAMERA_FACING_BACK);
}
if (isSupportFlashCamera) {
cameraFlash = CameraUtils.getCameraFlash(context);
}
}
private static CameraManager INSTANCE;
public static CameraManager getInstance(Application context) {
if (INSTANCE == null) {
synchronized (CameraManager.class) {
if (INSTANCE == null) {
INSTANCE = new CameraManager(context);
}
}
}
return INSTANCE;
}
/**
* camera
*/
public void openCamera(SurfaceTexture surfaceTexture, int width, int height) {
if (mCamera == null) {
mCamera = Camera.open(cameraFacing);//
mProfile = CamcorderProfile.get(cameraFacing, CamcorderProfile.QUALITY_HIGH);
try {
mCamera.setDisplayOrientation(90);//
mCamera.setPreviewTexture(surfaceTexture);
initCameraParameters(cameraFacing, width, height);
mCamera.startPreview();
} catch (Exception e) {
LogUtils.i(e);
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}
}
/**
* , camera
*/
public void restartPreview() {
if (mCamera == null) return;
try {
Camera.Parameters parameters = mCamera.getParameters();
int zoom = parameters.getZoom();
if (zoom > 0) {
parameters.setZoom(0);
mCamera.setParameters(parameters);
}
mCamera.startPreview();
} catch (Exception e) {
LogUtils.i(e);
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}
private void initCameraParameters(int cameraId, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes != null) {
if (cameraType == 0) {
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
} else {
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
}
}
}
parameters.setRotation(90);// ,
switch (cameraFlash) {
case 0:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
break;
case 1:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
break;
case 2:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
break;
}
List<Camera.Size> pictureSizes = parameters.getSupportedPictureSizes();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
if (!isEmpty(pictureSizes) && !isEmpty(previewSizes)) {
/**
for (Camera.Size size : pictureSizes) {
LogUtils.i("pictureSize " + size.width + " " + size.height);
}
for (Camera.Size size : pictureSizes) {
LogUtils.i("previewSize " + size.width + " " + size.height);
}*/
Camera.Size optimalPicSize = getOptimalCameraSize(pictureSizes, width, height);
Camera.Size optimalPreSize = getOptimalCameraSize(previewSizes, width, height);
// Camera.Size optimalPicSize = getOptimalSize(pictureSizes, width, height);
// Camera.Size optimalPreSize = getOptimalSize(previewSizes, width, height);
LogUtils.i("TextureSize "+width+" "+height+" optimalSize pic " + optimalPicSize.width + " " + optimalPicSize.height + " pre " + optimalPreSize.width + " " + optimalPreSize.height);
parameters.setPictureSize(optimalPicSize.width, optimalPicSize.height);
parameters.setPreviewSize(optimalPreSize.width, optimalPreSize.height);
mProfile.videoFrameWidth = optimalPreSize.width;
mProfile.videoFrameHeight = optimalPreSize.height;
mProfile.videoBitRate = 5000000;//
}
mCamera.setParameters(parameters);
}
/**
*
*/
public void closeCamera() {
this.cameraType = 0;
if (mCamera != null) {
try {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
} catch (Exception e) {
LogUtils.i(e);
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}
}
/**
*
*
* @param list
* @param <E>
* @return
*/
private <E> boolean isEmpty(List<E> list) {
return list == null || list.isEmpty();
}
/**
*
* @param sizes support
* @param w
* @param h
* @return Camera size
*/
private Camera.Size getOptimalCameraSize(List<Camera.Size> sizes, int w, int h){
sortCameraSize(sizes);
int position = binarySearch(sizes, w*h);
return sizes.get(position);
}
/**
*
* @param sizes
* @param targetNum
* @return
*/
private int binarySearch(List<Camera.Size> sizes,int targetNum){
int targetIndex;
int left = 0,right;
int length = sizes.size();
for (right = length-1;left != right;){
int midIndex = (right + left)/2;
int mid = right - left;
Camera.Size size = sizes.get(midIndex);
int midValue = size.width * size.height;
if (targetNum == midValue){
return midIndex;
}
if (targetNum > midValue){
left = midIndex;
}else {
right = midIndex;
}
if (mid <= 1){
break;
}
}
Camera.Size rightSize = sizes.get(right);
Camera.Size leftSize = sizes.get(left);
int rightNum = rightSize.width * rightSize.height;
int leftNum = leftSize.width * leftSize.height;
targetIndex = Math.abs((rightNum - leftNum)/2) > Math.abs(rightNum - targetNum) ? right : left;
return targetIndex;
}
/**
*
* @param previewSizes
*/
private void sortCameraSize(List<Camera.Size> previewSizes){
Collections.sort(previewSizes, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size size1, Camera.Size size2) {
int compareHeight = size1.height - size2.height;
if (compareHeight == 0){
return (size1.width == size2.width ? 0 :(size1.width > size2.width ? 1:-1));
}
return compareHeight;
}
});
}
/**
* Size
*
* @return
*/
private Camera.Size getOptimalSize(List<Camera.Size> sizes, int w, int h) {
Camera.Size optimalSize = null;
float targetRadio = h / (float) w;
float optimalDif = Float.MAX_VALUE; //
int optimalMaxDif = Integer.MAX_VALUE;//
for (Camera.Size size : sizes) {
float newOptimal = size.width / (float) size.height;
float newDiff = Math.abs(newOptimal - targetRadio);
if (newDiff < optimalDif) { //
optimalDif = newDiff;
optimalSize = size;
optimalMaxDif = Math.abs(h - size.width);
} else if (newDiff == optimalDif) {//
int newOptimalMaxDif = Math.abs(h - size.width);
if (newOptimalMaxDif < optimalMaxDif) {
optimalDif = newDiff;
optimalSize = size;
optimalMaxDif = newOptimalMaxDif;
}
}
}
return optimalSize;
}
/**
*
* @param isZoomIn
*/
public void handleZoom(boolean isZoomIn) {
if (mCamera == null) return;
Camera.Parameters params = mCamera.getParameters();
if (params == null) return;
if (params.isZoomSupported()) {
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
if (isZoomIn && zoom < maxZoom) {
zoom++;
} else if (zoom > 0) {
zoom--;
}
params.setZoom(zoom);
mCamera.setParameters(params);
} else {
LogUtils.i("zoom not supported");
}
}
/**
*
*/
public void changeCameraFacing(SurfaceTexture surfaceTexture, int width, int height) {
if(isSupportFrontCamera) {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int cameraCount = Camera.getNumberOfCameras();//
for(int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, cameraInfo);//
if(cameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT) { // ,
if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//
closeCamera();
cameraFacing = Camera.CameraInfo.CAMERA_FACING_BACK;
CameraUtils.setCameraFacing(context, cameraFacing);
openCamera(surfaceTexture, width, height);
break;
}
} else {// ,
if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {//
closeCamera();
cameraFacing = Camera.CameraInfo.CAMERA_FACING_FRONT;
CameraUtils.setCameraFacing(context, cameraFacing);
openCamera(surfaceTexture, width, height);
break;
}
}
}
} else { //
Toast.makeText(context, " ", Toast.LENGTH_SHORT).show();
}
}
/**
*
*/
public void changeCameraFlash(SurfaceTexture surfaceTexture, int width, int height) {
if (!isSupportFlashCamera) {
Toast.makeText(context, " ", Toast.LENGTH_SHORT).show();
return;
}
if(mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
if(parameters != null) {
int newState = cameraFlash;
switch (cameraFlash) {
case 0: //
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
newState = 1;
break;
case 1://open
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
newState = 2;
break;
case 2: //close
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
newState = 0;
break;
}
cameraFlash = newState;
CameraUtils.setCameraFlash(context, newState);
mCamera.setParameters(parameters);
}
}
}
/**
*
*/
public void takePhoto(Camera.PictureCallback callback) {
if (mCamera != null) {
try {
mCamera.takePicture(null, null, callback);
} catch(Exception e) {
Toast.makeText(context, " ", Toast.LENGTH_SHORT).show();
}
}
}
/**
*
*/
// public void startMediaRecord(String savePath) {
// if (mCamera == null || mProfile == null) return;
// mCamera.unlock();
// if (mMediaRecorder == null) {
// mMediaRecorder = new MediaRecorder();
// }
// if (isCameraFrontFacing()) {
// mMediaRecorder.setOrientationHint(270);
// Log.i("wujie","front");
// }else
// {
// Log.i("wujie","back");
// mMediaRecorder.setOrientationHint(90);
// }
// mMediaRecorder.reset();
// mMediaRecorder.setCamera(mCamera);
// mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// mMediaRecorder.setProfile(mProfile);
// mMediaRecorder.setOutputFile(savePath);
// try {
// mMediaRecorder.prepare();
// mMediaRecorder.start();
// } catch (Exception e) {
// e.printStackTrace();
//
// }
// }
/**
*
*/
public void startMediaRecord1(String savePath) {
if (mCamera == null) {
return;
}
if (mMediaRecorder == null) {
mMediaRecorder = new MediaRecorder();
} else {
mMediaRecorder.reset();
}
if (isCameraFrontFacing()) {
mMediaRecorder.setOrientationHint(270);
Log.i("wujie","front");
}else
{
Log.i("wujie","back");
mMediaRecorder.setOrientationHint(90);
}
mParameters = mCamera.getParameters();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//
mMediaRecorder.setProfile(CamcorderProfile.get(AppConfig.VIDEOSIZE));
try {
mMediaRecorder.setOutputFile(savePath);
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (Exception e) {
}
}
/**
*
*/
public void stopMediaRecord() {
this.cameraType = 0;
stopRecorder();
releaseMediaRecorder();
}
private void releaseMediaRecorder() {
if (mMediaRecorder != null) {
try {
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
mCamera.lock();
} catch (Exception e) {
e.printStackTrace();
LogUtils.i(e);
}
}
}
private void stopRecorder() {
if (mMediaRecorder != null) {
try {
mMediaRecorder.stop();
} catch (Exception e) {
e.printStackTrace();
LogUtils.i(e);
}
}
}
public boolean isSupportFrontCamera() {
return isSupportFrontCamera;
}
public boolean isSupportFlashCamera() {
return isSupportFlashCamera;
}
public boolean isCameraFrontFacing() {
return cameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT;
}
/**
*
* @param cameraType
*/
public void setCameraType(int cameraType) {
this.cameraType = cameraType;
if (mCamera != null) {//
if (cameraFacing == Camera.CameraInfo.CAMERA_FACING_BACK) {
Camera.Parameters parameters = mCamera.getParameters();
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes != null) {
if (cameraType == 0) {
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
} else {
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
}
}
}
}
}
public int getCameraFlash() {
return cameraFlash;
}
/**
*
* @param x
* @param y
*/
public void handleFocusMetering(float x, float y) {
if(mCamera!=null){
Camera.Parameters params = mCamera.getParameters();
Camera.Size previewSize = params.getPreviewSize();
Rect focusRect = calculateTapArea(x, y, 1f, previewSize);
Rect meteringRect = calculateTapArea(x, y, 1.5f, previewSize);
mCamera.cancelAutoFocus();
if (params.getMaxNumFocusAreas() > 0) {
List<Camera.Area> focusAreas = new ArrayList<>();
focusAreas.add(new Camera.Area(focusRect, 1000));
params.setFocusAreas(focusAreas);
} else {
LogUtils.i("focus areas not supported");
}
if (params.getMaxNumMeteringAreas() > 0) {
List<Camera.Area> meteringAreas = new ArrayList<>();
meteringAreas.add(new Camera.Area(meteringRect, 1000));
params.setMeteringAreas(meteringAreas);
} else {
LogUtils.i("metering areas not supported");
}
final String currentFocusMode = params.getFocusMode();
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.setParameters(params);
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Camera.Parameters params = camera.getParameters();
params.setFocusMode(currentFocusMode);
camera.setParameters(params);
}
});
}
}
private Rect calculateTapArea(float x, float y, float coefficient, Camera.Size previewSize) {
float focusAreaSize = 300;
int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();
int centerX = (int) (x / previewSize.width - 1000);
int centerY = (int) (y / previewSize.height - 1000);
int left = clamp(centerX - areaSize / 2, -1000, 1000);
int top = clamp(centerY - areaSize / 2, -1000, 1000);
RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);
return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom));
}
private int clamp(int x, int min, int max) {
if (x > max) {
return max;
}
if (x < min) {
return min;
}
return x;
}
}
사용자 정의 촬영,사진 찍 기 버튼:
package com.ses.im.app.chat.newcamera;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import com.ses.im.app.chat.R;
/**
* ,
*/
public class CameraProgressBar extends View {
/**
*
*/
public static final float DEF_SCALE = 0.75F;
/**
*
*/
private float scale = DEF_SCALE;
/**
*
*/
private int innerColor = Color.GRAY;
/**
*
*/
private int backgroundColor = Color.WHITE;
/**
*
*/
private int outerColor = Color.parseColor("#e9e9e9");
/**
*
*/
private int progressColor = Color.parseColor("#0ebffa");
/**
*
*/
private int progressWidth = 15;
/**
*
*/
private int innerRadio = 10;
/**
*
*/
private int progress;
/**
*
*/
private int maxProgress = 100;
/**
* paint
*/
private Paint backgroundPaint, progressPaint, innerPaint;
/**
* ,
*/
private float sweepAngle;
/**
*
*/
private GestureDetectorCompat mDetector;
/**
*
*/
private boolean isLongClick;
/**
*
*/
private boolean isBeingDrag;
/**
*
*/
private int mTouchSlop;
/**
* Y
*/
private float mLastY;
/**
*
*/
private boolean isLongScale;
public CameraProgressBar(Context context) {
super(context);
init(context, null);
}
public CameraProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CameraProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraProgressBar);
innerColor = a.getColor(R.styleable.CameraProgressBar_innerColor, innerColor);
outerColor = a.getColor(R.styleable.CameraProgressBar_outerColor, outerColor);
progressColor = a.getColor(R.styleable.CameraProgressBar_progressColor, progressColor);
innerRadio = a.getDimensionPixelOffset(R.styleable.CameraProgressBar_innerRadio, innerRadio);
progressWidth = a.getDimensionPixelOffset(R.styleable.CameraProgressBar_progressWidth, progressWidth);
progress = a.getInt(R.styleable.CameraProgressBar_progresscamera, progress);
scale = a.getFloat(R.styleable.CameraProgressBar_scale, scale);
isLongScale = a.getBoolean(R.styleable.CameraProgressBar_isLongScale, isLongScale);
maxProgress = a.getInt(R.styleable.CameraProgressBar_maxProgress, maxProgress);
a.recycle();
}
backgroundPaint = new Paint();
backgroundPaint.setAntiAlias(true);
backgroundPaint.setColor(backgroundColor);
progressPaint = new Paint();
progressPaint.setAntiAlias(true);
progressPaint.setStrokeWidth(progressWidth);
progressPaint.setStyle(Paint.Style.STROKE);
innerPaint = new Paint();
innerPaint.setAntiAlias(true);
innerPaint.setStrokeWidth(innerRadio);
innerPaint.setStyle(Paint.Style.STROKE);
sweepAngle = ((float) progress / maxProgress) * 360;
mDetector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
isLongClick = false;
if (CameraProgressBar.this.listener != null) {
CameraProgressBar.this.listener.onClick(CameraProgressBar.this);
}
return super.onSingleTapConfirmed(e);
}
@Override
public void onLongPress(MotionEvent e) {
isLongClick = true;
postInvalidate();
mLastY = e.getY();
if (CameraProgressBar.this.listener != null) {
CameraProgressBar.this.listener.onLongClick(CameraProgressBar.this);
}
}
});
mDetector.setIsLongpressEnabled(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width > height) {
setMeasuredDimension(height, height);
} else {
setMeasuredDimension(width, width);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
float circle = width / 2.0f;
if (/*isLongScale && */!isLongClick) {
canvas.scale(scale, scale, circle, circle);
}
//
float backgroundRadio = circle - progressWidth - innerRadio;
canvas.drawCircle(circle, circle, backgroundRadio, backgroundPaint);
//
float halfInnerWidth = innerRadio / 2.0f + progressWidth;
RectF innerRectF = new RectF(halfInnerWidth, halfInnerWidth, width - halfInnerWidth, width - halfInnerWidth);
canvas.drawArc(innerRectF, -90, 360, true, innerPaint);
progressPaint.setColor(outerColor);
float halfOuterWidth = progressWidth / 2.0f;
RectF outerRectF = new RectF(halfOuterWidth, halfOuterWidth, getWidth() - halfOuterWidth, getWidth() - halfOuterWidth);
canvas.drawArc(outerRectF, -90, 360, true, progressPaint);
progressPaint.setColor(progressColor);
canvas.drawArc(outerRectF, -90, sweepAngle, false, progressPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isLongScale) {
return super.onTouchEvent(event);
}
this.mDetector.onTouchEvent(event);
switch(MotionEventCompat.getActionMasked(event)) {
case MotionEvent.ACTION_DOWN:
isLongClick = false;
isBeingDrag = false;
break;
case MotionEvent.ACTION_MOVE:
if (isLongClick) {
float y = event.getY();
if (isBeingDrag) {
boolean isUpScroll = y < mLastY;
mLastY = y;
if (this.listener != null) {
this.listener.onZoom(isUpScroll);
}
} else {
isBeingDrag = Math.abs(y - mLastY) > mTouchSlop;
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
isBeingDrag = false;
if (isLongClick) {
isLongClick = false;
postInvalidate();
if (this.listener != null) {
this.listener.onLongClickUp(this);
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
if (isLongClick) {
if (this.listener != null) {
this.listener.onPointerDown(event.getRawX(), event.getRawY());
}
}
break;
}
return true;
}
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
Parcelable superData = super.onSaveInstanceState();
bundle.putParcelable("superData", superData);
bundle.putInt("progress", progress);
bundle.putInt("maxProgress", maxProgress);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
Parcelable superData = bundle.getParcelable("superData");
progress = bundle.getInt("progress");
maxProgress = bundle.getInt("maxProgress");
super.onRestoreInstanceState(superData);
}
/**
*
* @param progress
*/
public void setProgress(int progress) {
if (progress <= 0) progress = 0;
if (progress >= maxProgress) progress = maxProgress;
if (progress == this.progress) return;
this.progress = progress;
this.sweepAngle = ((float) progress / maxProgress) * 360;
postInvalidate();
}
/**
*
*/
public void reset() {
isLongClick = false;
this.progress = 0;
this.sweepAngle = 0;
postInvalidate();
}
public int getProgress() {
return progress;
}
public void setLongScale(boolean longScale) {
isLongScale = longScale;
}
public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress;
}
private OnProgressTouchListener listener;
public void setOnProgressTouchListener(OnProgressTouchListener listener) {
this.listener = listener;
}
/**
*
*/
public interface OnProgressTouchListener {
/**
*
* @param progressBar
*/
void onClick(CameraProgressBar progressBar);
/**
*
* @param progressBar
*/
void onLongClick(CameraProgressBar progressBar);
/**
*
* @param zoom true
*/
void onZoom(boolean zoom);
/**
*
* @param progressBar
*/
void onLongClickUp(CameraProgressBar progressBar);
/**
*
* @param rawX
* @param rawY
*/
void onPointerDown(float rawX, float rawY);
}
}
미리 보기 시도 페이지:
package com.ses.im.app.chat.newcamera;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
/**
*
*/
public class CameraView extends View {
/**
*
*/
private static final int ANIM_MILS = 600;
/**
*
*/
private static final int ANIM_UPDATE = 30;
/**
* focus paint
*/
private Paint paint, clearPaint;
private int paintColor = Color.GREEN;
/**
*
*/
private Subscription subscription;
/**
* focus rectf
*/
private RectF rectF = new RectF();
/**
* focus size
*/
private int focusSize = 120;
private int lineSize = focusSize / 4;
/**
*
*/
private float oldDist = 1f;
/**
*
*/
private float paintWidth = 6.0f;
/**
* s
*/
private float scale;
public CameraView(Context context) {
super(context);
init();
}
public CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setColor(paintColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(paintWidth);
clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
if (event.getPointerCount() == 1 && action == MotionEvent.ACTION_DOWN) {
float x = event.getX();
float y = event.getY();
setFoucsPoint(x, y);
if (listener != null) {
listener.handleFocus(x, y);
}
} else if (event.getPointerCount() >= 2){
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = getFingerSpacing(event);
break;
case MotionEvent.ACTION_MOVE:
float newDist = getFingerSpacing(event);
if (newDist > oldDist) {
if (this.listener != null) {
this.listener.handleZoom(true);
}
} else if (newDist < oldDist) {
if (this.listener != null) {
this.listener.handleZoom(false);
}
}
oldDist = newDist;
break;
}
}
return true;
}
/**
*
* @param event
* @return
*/
private float getFingerSpacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
/**
* ( rawX, rawY)
*/
public void setFoucsPoint(PointF pointF) {
PointF transPointF = transPointF(pointF, this);
setFoucsPoint(transPointF.x, transPointF.y);
}
/**
*
* @param x
* @param y
*/
private void setFoucsPoint(float x, float y) {
if (subscription != null) {
subscription.unsubscribe();
}
rectF.set(x - focusSize, y - focusSize, x + focusSize, y + focusSize);
final int count = ANIM_MILS / ANIM_UPDATE;
subscription = Observable.interval(ANIM_UPDATE, TimeUnit.MILLISECONDS).take(count).subscribe(new Subscriber<Long>() {
@Override
public void onCompleted() {
scale = 0;
postInvalidate();
}
@Override
public void onError(Throwable e) {
scale = 0;
postInvalidate();
}
@Override
public void onNext(Long aLong) {
float current = aLong== null ? 0 : aLong.longValue();
scale = 1 - current / count;
if (scale <= 0.5f) {
scale = 0.5f;
}
postInvalidate();
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (scale != 0) {
float centerX = rectF.centerX();
float centerY = rectF.centerY();
canvas.scale(scale, scale, centerX, centerY);
canvas.drawRect(rectF, paint);
canvas.drawLine(rectF.left, centerY, rectF.left + lineSize, centerY, paint);
canvas.drawLine(rectF.right, centerY, rectF.right - lineSize, centerY, paint);
canvas.drawLine(centerX, rectF.top, centerX, rectF.top + lineSize, paint);
canvas.drawLine(centerX, rectF.bottom, centerX, rectF.bottom - lineSize, paint);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (subscription != null) {
subscription.unsubscribe();
}
}
/**
* raw
* @param pointF
* @return
*/
private PointF transPointF(PointF pointF, View view) {
pointF.x -= view.getX();
pointF.y -= view.getY();
ViewParent parent = view.getParent();
if (parent instanceof View) {
return transPointF(pointF, (View) parent);
} else {
return pointF;
}
}
private OnViewTouchListener listener;
public void setOnViewTouchListener(OnViewTouchListener listener) {
this.listener = listener;
}
public void removeOnZoomListener() {
this.listener = null;
}
public interface OnViewTouchListener {
/**
*
* @param x
* @param y
*/
void handleFocus(float x, float y);
/**
*
* @param zoom true
*/
void handleZoom(boolean zoom);
}
}
비디오 관리 클래스:
package com.ses.im.app.chat.newcamera;
import android.app.Application;
import android.media.MediaPlayer;
import android.view.Surface;
/**
* Created by you on 2016/10/24.
* TextureView, mediaplayer
*/
public class MediaPlayerManager {
private Application app;
private MediaPlayer mPlayer;
private MediaPlayerManager(Application app) {
this.app = app;
}
private static MediaPlayerManager INSTANCE;
public static MediaPlayerManager getInstance(Application app) {
if (INSTANCE == null) {
synchronized (CameraManager.class) {
if (INSTANCE == null) {
INSTANCE = new MediaPlayerManager(app);
}
}
}
return INSTANCE;
}
/**
* Media
*/
public void playMedia(Surface surface, String mediaPath) {
try {
if (mPlayer == null) {
mPlayer = new MediaPlayer();
mPlayer.setDataSource(mediaPath);
} else {
if (mPlayer.isPlaying()) {
mPlayer.stop();
}
mPlayer.reset();
mPlayer.setDataSource(mediaPath);
}
mPlayer.setSurface(surface);
mPlayer.setLooping(true);
mPlayer.prepareAsync();
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} catch (Exception e) {
LogUtils.i(e);
}
}
/**
* Media
*/
public void stopMedia() {
try {
if (mPlayer != null) {
if (mPlayer.isPlaying()) {
mPlayer.stop();
}
mPlayer.release();
mPlayer = null;
}
} catch (Exception e) {
LogUtils.i(e);
}
}
}
본인 이 올 린 demo 가 csdn 에 의 해 잃 어 버 렸 기 때문에 위 챗 카 메 라 를 만 들 려 면 참고 하 세 요진가 동의 demo이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.