Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.digipro.fesdkcore.Componentes;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import androidx.databinding.DataBindingUtil;
- import androidx.databinding.ObservableField;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Looper;
- import com.google.android.material.floatingactionbutton.FloatingActionButton;
- import androidx.appcompat.app.AppCompatActivity;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.digipro.fesdkcore.FeSdkCore;
- import com.digipro.fesdkcore.R;
- import com.digipro.fesdkcore.R2;
- import com.digipro.fesdkcore.databinding.ComponenteCameravideoBinding;
- import com.digipro.fesdkcore.dto.Constantes;
- import com.digipro.fesdkcore.repositorios.LogErrorRepository;
- import com.digipro.fesdkcore.utils.Extension;
- import com.digipro.fesdkcore.widgets.Spotlight.prefs.PreferencesManager;
- import com.digipro.fesdkcore.widgets.Spotlight.utils.SpotlightSequence;
- import com.digipro.fesdkcore.widgets.Spotlight.utils.Utils;
- import com.otaliastudios.cameraview.CameraListener;
- import com.otaliastudios.cameraview.CameraLogger;
- import com.otaliastudios.cameraview.CameraOptions;
- import com.otaliastudios.cameraview.CameraView;
- import com.otaliastudios.cameraview.Facing;
- import com.otaliastudios.cameraview.SessionType;
- import com.otaliastudios.cameraview.Size;
- import com.otaliastudios.cameraview.VideoQuality;
- import java.io.File;
- import java.io.FileOutputStream;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import es.dmoral.toasty.Toasty;
- import static com.otaliastudios.cameraview.Flash.OFF;
- import static com.otaliastudios.cameraview.Flash.TORCH;
- public class Camera extends AppCompatActivity {
- private static final String IDbtnstopVideo = "IDbtnstopVideo";
- private static final String IDtxttextviewanchorstext = "IDtxttextviewanchorstext";
- private static final String IDbtncaptureVideo = "IDbtncaptureVideo";
- private static final String IDbtncapturePhoto = "IDbtncapturePhoto";
- private static final String IDbtntoggleCamera = "IDbtntoggleCamera";
- private static final String IDbtnfab_flash = "IDbtnfab_flash";
- private static final String IDbtnfab_cancelar_camara = "IDbtnfab_cancelar_camara";
- private static Handler handler = new Handler(Looper.getMainLooper());
- private static Runnable runnable;
- public ObservableField<String> textreadvideo = new ObservableField<>();
- @BindView(R2.id.camera)
- CameraView camera;
- Context mContext;
- @BindView(R2.id.capturePhoto)
- FloatingActionButton capturePhoto;
- @BindView(R2.id.captureVideo)
- FloatingActionButton captureVideo;
- @BindView(R2.id.stopVideo)
- FloatingActionButton stopVideo;
- @BindView(R2.id.toggleCamera)
- FloatingActionButton toggleCamera;
- // private ViewGroup controlPanel;
- @BindView(R2.id.fab_flash)
- FloatingActionButton fab_flash;
- @BindView(R2.id.textviewanchorstext)
- TextView textviewanchorstext;
- @BindView(R2.id.ayudaFoto)
- FloatingActionButton ayudaFoto;
- @BindView(R2.id.fab_cancelar_camara)
- FloatingActionButton fab_cancelar_camara;
- boolean deviceHasFlash = true;
- boolean deviceHasFrontCamera = true;
- boolean expand = true;
- private Uri uriPath;
- private boolean flashEnabled = false;
- private boolean camaraTrasera = true;
- private boolean mCapturingPicture;
- private boolean mCapturingVideo;
- // To show stuff in the callback
- private Size mCaptureNativeSize;
- private long mCaptureTime;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- try {
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
- WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
- CameraLogger.setLogLevel(CameraLogger.LEVEL_VERBOSE);
- ComponenteCameravideoBinding binding = DataBindingUtil.setContentView(this, R.layout.componente_cameravideo);
- ButterKnife.bind(this);
- startSendScreenNameAnalytics();
- mContext = getBaseContext();
- String fotoVideo = getIntent().getStringExtra("fotoVideo");
- String modocamara = getIntent().getStringExtra("modocamara");
- String leyendavideo = getIntent().getStringExtra("leyendavideo");
- if (fotoVideo.equals("VIDEO")) {
- if (leyendavideo.isEmpty()) textviewanchorstext.setVisibility(View.GONE);
- else textviewanchorstext.setVisibility(View.VISIBLE);
- }
- binding.setTextreadvideo(textreadvideo);
- textreadvideo.set(leyendavideo);
- ayudaFoto.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- expand = !expand;
- PreferencesManager mPreferencesManager = new PreferencesManager(mContext);
- mPreferencesManager.resetAll();
- if (fotoVideo.equals("FOTO")) {
- showSecuenseHintFoto();
- } else {
- showSecuenseHintVideo();
- }
- }
- });
- checkHasFlash();
- checkHasFrontCamera();
- if (!modocamara.isEmpty() && modocamara.equals("front") && deviceHasFrontCamera) {
- camera.setFacing(Facing.FRONT);
- camaraTrasera = false;
- } else {
- camaraTrasera = true;
- camera.setFacing(Facing.BACK);
- }
- fab_flash.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- flashEnabled = !flashEnabled;
- if (flashEnabled)
- camera.setFlash(TORCH);
- else
- camera.setFlash(OFF);
- }
- });
- if (fotoVideo.equals("FOTO")) {
- camera.setSessionType(SessionType.PICTURE);
- textviewanchorstext.setVisibility(View.GONE);
- capturePhoto.setVisibility(View.VISIBLE);
- captureVideo.setVisibility(View.GONE);
- } else {
- camera.setSessionType(SessionType.VIDEO);
- camera.setVideoMaxSize(4000 * 1000); // 4 mb
- camera.setVideoQuality(VideoQuality.MAX_480P);
- capturePhoto.setVisibility(View.GONE);
- captureVideo.setVisibility(View.VISIBLE);
- }
- camera.addCameraListener(new CameraListener() {
- public void onCameraOpened(CameraOptions options) {
- // onOpened();
- }
- public void onPictureTaken(byte[] jpeg) {
- onPicture(jpeg);
- }
- @Override
- public void onVideoTaken(File video) {
- super.onVideoTaken(video);
- onVideo(video);
- }
- });
- capturePhoto.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ayudaFoto.setVisibility(View.GONE);
- capturePhoto();
- }
- });
- captureVideo.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ayudaFoto.setVisibility(View.GONE);
- captureVideo();
- }
- });
- toggleCamera.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- toggleCamera();
- }
- });
- stopVideo.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (camera.isCapturingVideo())
- camera.stopCapturingVideo();
- // ayudaFoto.setVisibility(View.VISIBLE);
- // fab_cancelar_camara.setVisibility(View.VISIBLE);
- }
- });
- // controlPanel = findViewById(R.id.controls);
- // ViewGroup group = (ViewGroup) controlPanel.getChildAt(0);
- // Control[] controls = Control.values();
- // for (Control control : controls) {
- // ControlView view = new ControlView(this, control, this);
- // group.addView(view, ViewGroup.LayoutParams.MATCH_PARENT,
- // ViewGroup.LayoutParams.WRAP_CONTENT);
- // }
- // controlPanel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
- // @Override
- // public void onGlobalLayout() {
- // BottomSheetBehavior b = BottomSheetBehavior.from(controlPanel);
- // b.setState(BottomSheetBehavior.STATE_HIDDEN);
- // }
- // });
- fab_cancelar_camara.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- finish();
- }
- });
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- private void checkHasFlash() {
- try {
- deviceHasFlash = mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
- if (deviceHasFlash)
- if (camaraTrasera)
- fab_flash.setVisibility(View.VISIBLE);
- else
- fab_flash.setVisibility(View.GONE);
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- private void checkHasFrontCamera() {
- try {
- deviceHasFrontCamera = mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
- if (deviceHasFrontCamera){
- toggleCamera.setVisibility(View.VISIBLE);
- }
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- private void message(String content, boolean important) {
- try {
- int length = important ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
- Toast.makeText(this, content, length).show();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- // private void onOpened() {
- // ViewGroup group = (ViewGroup) controlPanel.getChildAt(0);
- // for (int i = 0; i < group.getChildCount(); i++) {
- // ControlView view = (ControlView) group.getChildAt(i);
- // view.onCameraOpened(camera);
- // }
- // }
- private void onPicture(byte[] jpeg) {
- try {
- mCapturingPicture = false;
- long callbackTime = System.currentTimeMillis();
- if (mCapturingVideo) {
- message("Captured while taking video. Size=" + mCaptureNativeSize, false);
- return;
- }
- // This can happen if picture was taken with a gesture.
- if (mCaptureTime == 0) mCaptureTime = callbackTime - 300;
- if (mCaptureNativeSize == null) mCaptureNativeSize = camera.getPictureSize();
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 4;
- Bitmap originalBitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length, options);
- File file = Extension.createFile(Constantes.DB_Usuario + FeSdkCore.configSaved.LastCodigoUsed + "_" + FeSdkCore.usuarioSaved.User + Constantes.DB_Anexo, Extension.getFormatDate("yyyyMMddHHmmssSSS"), getBaseContext());
- FileOutputStream out = new FileOutputStream(file);
- originalBitmap.compress(Bitmap.CompressFormat.PNG, 75, out); // bmp is your Bitmap instance
- out.close();
- uriPath = Uri.fromFile(file);
- mCaptureTime = 0;
- mCaptureNativeSize = null;
- saveResult();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(getBaseContext(), mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- private void onVideo(File video) {
- try {
- mCapturingVideo = false;
- uriPath = Uri.fromFile(video);
- saveResult();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- @Override
- public void onBackPressed() {
- try {
- finish();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- private void capturePhoto() {
- try {
- if (mCapturingPicture) return;
- mCapturingPicture = true;
- mCaptureTime = System.currentTimeMillis();
- mCaptureNativeSize = camera.getPictureSize();
- message("Capturando", false);
- camera.capturePicture();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- private void captureVideo() {
- try {
- if (camera.getSessionType() != SessionType.VIDEO) {
- message("No es posible grabar si se encuentra en Foto", false);
- return;
- }
- ayudaFoto.setVisibility(View.GONE);
- fab_cancelar_camara.setVisibility(View.GONE);
- if (mCapturingPicture || mCapturingVideo) return;
- mCapturingVideo = true;
- message("Grabando...", false);
- runnable = new Runnable() {
- @Override
- public void run() {
- try {
- stopVideo.setVisibility(View.VISIBLE);
- } catch (Exception ex) {
- LogErrorRepository.BuildLogError(ex);
- }
- handler.removeCallbacks(runnable);
- }
- };
- handler.postDelayed(runnable, 1000);
- toggleCamera.setVisibility(View.GONE);
- captureVideo.setVisibility(View.GONE);
- // message("Recording for 8 seconds...", true);
- File file = Extension.createFile(Constantes.DB_Usuario + FeSdkCore.configSaved.LastCodigoUsed + "_" + FeSdkCore.usuarioSaved.User + Constantes.DB_Anexo, Extension.getFormatDate("yyyyMMddHHmmssSSS"), getBaseContext());
- camera.startCapturingVideo(file, 8000);
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(getBaseContext(), mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- private void toggleCamera() {
- try {
- if (mCapturingPicture) return;
- switch (camera.toggleFacing()) {
- case BACK:
- // message("Camara Trasera", false);
- camaraTrasera = true;
- checkHasFlash();
- break;
- case FRONT:
- // message("Camara Frontal", false);
- camaraTrasera = false;
- checkHasFlash();
- break;
- }
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- //endregion
- private void startSendScreenNameAnalytics() {
- // startGoogleAnalytics(this);
- // AnalyticsUtilssendScreenName(Camera.class.getSimpleName());
- }
- public void saveResult() {
- try {
- Intent intent = new Intent();
- intent.setData(uriPath);
- setResult(RESULT_OK, intent);
- super.onBackPressed();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- private void showSecuenseHintFoto() {
- int visibilityfab_flash = fab_flash.getVisibility();
- int visibilitytoggleCamera = toggleCamera.getVisibility();
- int visibilitycapturePhoto = capturePhoto.getVisibility();
- int visibilityfab_cancelar_camara = fab_cancelar_camara.getVisibility();
- new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
- @Override
- public void run() {
- // showSequence.resetSpotlights(mContext);
- Utils.mostrar_ocultar_boton(null, 0, fab_flash, visibilityfab_flash);
- SpotlightSequence.getInstance(Camera.this, null)
- .addSpotlight(fab_flash, "Flash", "Activar flash", IDbtnfab_flash, Utils.configSpotlightColor(fab_flash.getBackgroundTintList().getDefaultColor()), () -> {
- Utils.mostrar_ocultar_boton(fab_flash, visibilityfab_flash, toggleCamera, visibilitytoggleCamera);
- })
- .addSpotlight(toggleCamera, "Frontal/Trasera", "Cambiar camara Frontal/Trasera", IDbtntoggleCamera, Utils.configSpotlightColor(toggleCamera.getBackgroundTintList().getDefaultColor()), () -> {
- Utils.mostrar_ocultar_boton(toggleCamera, visibilitytoggleCamera, capturePhoto, visibilitycapturePhoto);
- })
- .addSpotlight(capturePhoto, "Capturar", "Capturar foto", IDbtncapturePhoto, Utils.configSpotlightColor(capturePhoto.getBackgroundTintList().getDefaultColor()), () -> {
- Utils.mostrar_ocultar_boton(capturePhoto, visibilitycapturePhoto, null, 0);
- })
- .addSpotlight(fab_cancelar_camara, mContext.getResources().getString(R.string.Spotlight_Title_Cancelar), mContext.getResources().getString(R.string.Spotlight_Subtitle_Cancelar), IDbtnfab_cancelar_camara, Utils.configSpotlightColor(fab_cancelar_camara.getBackgroundTintList().getDefaultColor()), () -> {
- Utils.mostrar_ocultar_boton(fab_cancelar_camara, visibilityfab_cancelar_camara, null, 0);
- })
- .startSequence();
- }
- }, 200);
- }
- private void showSecuenseHintVideo() {
- int visibilityfab_flash = fab_flash.getVisibility();
- int visibilitytoggleCamera = toggleCamera.getVisibility();
- int visibilitycaptureVideo = captureVideo.getVisibility();
- int visibilitystopVideo = stopVideo.getVisibility();
- int visibilityfab_cancelar_camara = fab_cancelar_camara.getVisibility();
- new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
- @Override
- public void run() {
- SpotlightSequence.getInstance(Camera.this, null)
- .addSpotlight(textviewanchorstext, "Leyenda", "Texto a leer durante la grabacion", IDtxttextviewanchorstext, () -> Utils.mostrar_ocultar_boton(null, 0, fab_flash, visibilityfab_flash))
- .addSpotlight(fab_flash, "Flash", "Activar fash", IDbtnfab_flash, Utils.configSpotlightColor(fab_flash.getBackgroundTintList().getDefaultColor()), () -> Utils.mostrar_ocultar_boton(fab_flash, visibilityfab_flash, toggleCamera, visibilitytoggleCamera))
- .addSpotlight(toggleCamera, "Frontal/Trasera", "Cambiar camara Frontal/Trasera", IDbtntoggleCamera, Utils.configSpotlightColor(toggleCamera.getBackgroundTintList().getDefaultColor()), () -> {
- Utils.mostrar_ocultar_boton(null, 0, captureVideo, visibilitycaptureVideo);
- })
- .addSpotlight(captureVideo, "Grabar", "Grabar video", IDbtncaptureVideo, Utils.configSpotlightColor(captureVideo.getBackgroundTintList().getDefaultColor()), () -> {
- captureVideo.setVisibility(View.GONE);
- Utils.mostrar_ocultar_boton(null, 0, stopVideo, visibilitystopVideo);
- })
- .addSpotlight(stopVideo, "Detener", "Detener grabación", IDbtnstopVideo, Utils.configSpotlightColor(stopVideo.getBackgroundTintList().getDefaultColor()), () -> {
- Utils.mostrar_ocultar_boton(captureVideo, visibilitycaptureVideo, fab_cancelar_camara, visibilityfab_cancelar_camara);
- resetVisiblility(visibilityfab_flash, visibilitytoggleCamera, visibilitycaptureVideo, visibilitystopVideo);
- })
- .addSpotlight(fab_cancelar_camara, mContext.getResources().getString(R.string.Spotlight_Title_Cancelar), mContext.getResources().getString(R.string.Spotlight_Subtitle_Cancelar), IDbtnfab_cancelar_camara, Utils.configSpotlightColor(fab_cancelar_camara.getBackgroundTintList().getDefaultColor()), () -> {
- Utils.mostrar_ocultar_boton(fab_cancelar_camara, visibilityfab_cancelar_camara, null, 0);
- })
- .startSequence();
- }
- }, 200);
- }
- void resetVisiblility(int visibilityfab_flash, int visibilitytoggleCamera, int visibilitycaptureVideo, int visibilitystopVideo) {
- fab_flash.setVisibility(visibilityfab_flash);
- toggleCamera.setVisibility(visibilitytoggleCamera);
- captureVideo.setVisibility(visibilitycaptureVideo);
- stopVideo.setVisibility(visibilitystopVideo);
- }
- @Override
- protected void onResume() {
- try {
- super.onResume();
- camera.start();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- @Override
- protected void onPause() {
- try {
- super.onPause();
- camera.destroy();
- super.onBackPressed();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- @Override
- protected void onDestroy() {
- try {
- super.onDestroy();
- camera.destroy();
- } catch (Exception ex) {
- // AnalyticsUtilssendException(ex, true);
- LogErrorRepository.BuildLogError(ex);
- Toasty.error(mContext, mContext.getString(R.string.Mensaje_ErrorInterno), Toast.LENGTH_SHORT, true).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement