Can't use photo callback without cameraFragmentResultListener #11

pull/17/head
florent champigny 8 years ago
parent a7dc19664f
commit 6a8db6fc69
  1. 97
      app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java
  2. 8
      camerafragment/src/main/java/com/github/florent37/camerafragment/internal/ui/BaseAnncaFragment.java

@ -1,7 +1,6 @@
package com.github.florent37.camerafragment.sample; package com.github.florent37.camerafragment.sample;
import android.Manifest; import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -12,21 +11,14 @@ import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.github.florent37.camerafragment.CameraFragment; import com.github.florent37.camerafragment.CameraFragment;
import com.github.florent37.camerafragment.CameraFragmentApi; import com.github.florent37.camerafragment.CameraFragmentApi;
import com.github.florent37.camerafragment.configuration.Configuration; import com.github.florent37.camerafragment.configuration.Configuration;
import com.github.florent37.camerafragment.PreviewActivity;
import com.github.florent37.camerafragment.listeners.CameraFragmentStateListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentControlsListener; import com.github.florent37.camerafragment.listeners.CameraFragmentControlsListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener; import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentStateListener;
import com.github.florent37.camerafragment.listeners.CameraFragmentVideoRecordTextListener; import com.github.florent37.camerafragment.listeners.CameraFragmentVideoRecordTextListener;
import com.github.florent37.camerafragment.widgets.CameraSettingsView; import com.github.florent37.camerafragment.widgets.CameraSettingsView;
import com.github.florent37.camerafragment.widgets.CameraSwitchView; import com.github.florent37.camerafragment.widgets.CameraSwitchView;
@ -34,24 +26,39 @@ import com.github.florent37.camerafragment.widgets.FlashSwitchView;
import com.github.florent37.camerafragment.widgets.MediaActionSwitchView; import com.github.florent37.camerafragment.widgets.MediaActionSwitchView;
import com.github.florent37.camerafragment.widgets.RecordButton; import com.github.florent37.camerafragment.widgets.RecordButton;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
public static final String FRAGMENT_TAG = "camera";
private static final int REQUEST_CAMERA_PERMISSIONS = 931; private static final int REQUEST_CAMERA_PERMISSIONS = 931;
private static final int REQUEST_PREVIEW_CODE = 1001; private static final int REQUEST_PREVIEW_CODE = 1001;
@Bind(R.id.settings_view)
public static final String FRAGMENT_TAG = "camera"; CameraSettingsView settingsView;
@Bind(R.id.flash_switch_view)
@Bind(R.id.settings_view) CameraSettingsView settingsView; FlashSwitchView flashSwitchView;
@Bind(R.id.flash_switch_view) FlashSwitchView flashSwitchView; @Bind(R.id.front_back_camera_switcher)
@Bind(R.id.front_back_camera_switcher) CameraSwitchView cameraSwitchView; CameraSwitchView cameraSwitchView;
@Bind(R.id.record_button) RecordButton recordButton; @Bind(R.id.record_button)
@Bind(R.id.photo_video_camera_switcher) MediaActionSwitchView mediaActionSwitchView; RecordButton recordButton;
@Bind(R.id.photo_video_camera_switcher)
@Bind(R.id.record_duration_text) TextView recordDurationText; MediaActionSwitchView mediaActionSwitchView;
@Bind(R.id.record_size_mb_text) TextView recordSizeText;
@Bind(R.id.record_duration_text)
@Bind(R.id.cameraLayout) View cameraLayout; TextView recordDurationText;
@Bind(R.id.addCameraButton) View addCameraButton; @Bind(R.id.record_size_mb_text)
TextView recordSizeText;
@Bind(R.id.cameraLayout)
View cameraLayout;
@Bind(R.id.addCameraButton)
View addCameraButton;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -61,7 +68,7 @@ public class MainActivity extends AppCompatActivity {
} }
@OnClick(R.id.flash_switch_view) @OnClick(R.id.flash_switch_view)
public void onFlashSwitcClicked(){ public void onFlashSwitcClicked() {
final CameraFragmentApi cameraFragment = getCameraFragment(); final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.toggleFlashMode(); cameraFragment.toggleFlashMode();
@ -69,7 +76,7 @@ public class MainActivity extends AppCompatActivity {
} }
@OnClick(R.id.front_back_camera_switcher) @OnClick(R.id.front_back_camera_switcher)
public void onSwitchCameraClicked(){ public void onSwitchCameraClicked() {
final CameraFragmentApi cameraFragment = getCameraFragment(); final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.switchCameraTypeFrontBack(); cameraFragment.switchCameraTypeFrontBack();
@ -77,25 +84,25 @@ public class MainActivity extends AppCompatActivity {
} }
@OnClick(R.id.record_button) @OnClick(R.id.record_button)
public void onRecordButtonClicked(){ public void onRecordButtonClicked() {
final CameraFragmentApi cameraFragment = getCameraFragment(); final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.takePhotoOrCaptureVideo(new CameraFragmentResultListener() { cameraFragment.takePhotoOrCaptureVideo(new CameraFragmentResultListener() {
@Override @Override
public void onVideoRecorded(String filePath) { public void onVideoRecorded(String filePath) {
Toast.makeText(getBaseContext(), "onVideoRecorded " + filePath, Toast.LENGTH_SHORT).show();
} }
@Override @Override
public void onPhotoTaken(byte[] bytes, String filePath) { public void onPhotoTaken(byte[] bytes, String filePath) {
Toast.makeText(getBaseContext(), "onPhotoTaken " + filePath, Toast.LENGTH_SHORT).show();
} }
}); });
} }
} }
@OnClick(R.id.settings_view) @OnClick(R.id.settings_view)
public void onSettingsClicked(){ public void onSettingsClicked() {
final CameraFragmentApi cameraFragment = getCameraFragment(); final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.openSettingDialog(); cameraFragment.openSettingDialog();
@ -103,7 +110,7 @@ public class MainActivity extends AppCompatActivity {
} }
@OnClick(R.id.photo_video_camera_switcher) @OnClick(R.id.photo_video_camera_switcher)
public void onMediaActionSwitchClicked(){ public void onMediaActionSwitchClicked() {
final CameraFragmentApi cameraFragment = getCameraFragment(); final CameraFragmentApi cameraFragment = getCameraFragment();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.switchActionPhotoVideo(); cameraFragment.switchActionPhotoVideo();
@ -111,7 +118,7 @@ public class MainActivity extends AppCompatActivity {
} }
@OnClick(R.id.addCameraButton) @OnClick(R.id.addCameraButton)
public void onAddCameraClicked(){ public void onAddCameraClicked() {
if (Build.VERSION.SDK_INT > 15) { if (Build.VERSION.SDK_INT > 15) {
final String[] permissions = { final String[] permissions = {
Manifest.permission.CAMERA, Manifest.permission.CAMERA,
@ -153,19 +160,19 @@ public class MainActivity extends AppCompatActivity {
.commit(); .commit();
if (cameraFragment != null) { if (cameraFragment != null) {
cameraFragment.setResultListener(new CameraFragmentResultListener() { //cameraFragment.setResultListener(new CameraFragmentResultListener() {
@Override // @Override
public void onVideoRecorded(String filePath) { // public void onVideoRecorded(String filePath) {
Intent intent = PreviewActivity.newIntentVideo(MainActivity.this, filePath); // Intent intent = PreviewActivity.newIntentVideo(MainActivity.this, filePath);
startActivityForResult(intent, REQUEST_PREVIEW_CODE); // startActivityForResult(intent, REQUEST_PREVIEW_CODE);
} // }
//
@Override // @Override
public void onPhotoTaken(byte[] bytes, String filePath) { // public void onPhotoTaken(byte[] bytes, String filePath) {
Intent intent = PreviewActivity.newIntentPhoto(MainActivity.this, filePath); // Intent intent = PreviewActivity.newIntentPhoto(MainActivity.this, filePath);
startActivityForResult(intent, REQUEST_PREVIEW_CODE); // startActivityForResult(intent, REQUEST_PREVIEW_CODE);
} // }
}); //});
cameraFragment.setStateListener(new CameraFragmentStateListener() { cameraFragment.setStateListener(new CameraFragmentStateListener() {

@ -197,14 +197,14 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
@Override @Override
public void onPhotoTaken(byte[] bytes, CameraFragmentResultListener callback) { public void onPhotoTaken(byte[] bytes, CameraFragmentResultListener callback) {
if (cameraFragmentResultListener != null) {
final String filePath = cameraController.getOutputFile().toString(); final String filePath = cameraController.getOutputFile().toString();
if (cameraFragmentResultListener != null) {
cameraFragmentResultListener.onPhotoTaken(bytes, filePath); cameraFragmentResultListener.onPhotoTaken(bytes, filePath);
}
if (callback != null) { if (callback != null) {
callback.onPhotoTaken(bytes, filePath); callback.onPhotoTaken(bytes, filePath);
} }
} }
}
@Override @Override
public void onVideoRecordStart(int width, int height) { public void onVideoRecordStart(int width, int height) {
@ -421,7 +421,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
this.cameraController.switchCamera(cameraFace); this.cameraController.switchCamera(cameraFace);
} }
protected void setCameraTypeFrontBack(@Configuration.CameraFace int cameraFace){ protected void setCameraTypeFrontBack(@Configuration.CameraFace int cameraFace) {
switch (cameraFace) { switch (cameraFace) {
case Configuration.CAMERA_FACE_FRONT: case Configuration.CAMERA_FACE_FRONT:
currentCameraType = Camera.CAMERA_TYPE_FRONT; currentCameraType = Camera.CAMERA_TYPE_FRONT;
@ -705,7 +705,7 @@ public abstract class BaseAnncaFragment<CameraId> extends Fragment implements Ca
if (fileObserver != null) if (fileObserver != null)
fileObserver.stopWatching(); fileObserver.stopWatching();
if(countDownTimer != null) { if (countDownTimer != null) {
countDownTimer.stop(); countDownTimer.stop();
} }

Loading…
Cancel
Save