Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added camera switch mode: Front/Back #97

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@ RxPaparazzo.multiple(activityOrFragment)
.size(new ScreenSize())
.usingGallery()
```


### Camera mode
Switching camera for front or back. By default back set
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very good with English 😓 , but maybe this sentence is clearer, wdyt?

Switching the front or back camera. By default the back one is set

And what about adding a reminder to put this lines in the manifest?:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />

```java
RxPaparazzo.single(activityOrFragment)
.setCameraMode(CameraMode.FRONT)
.usingCamera()
```

### Cropping support for image.
This feature is available thanks to the amazing library [uCrop](https://github.com/Yalantis/uCrop) authored by [Yalantis](https://github.com/Yalantis) group.

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package="com.miguelbcr.ui.rx_paparazzo2.sample">

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :)


<application
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;
import com.miguelbcr.ui.rx_paparazzo2.RxPaparazzo;
import com.miguelbcr.ui.rx_paparazzo2.entities.CameraMode;
import com.miguelbcr.ui.rx_paparazzo2.entities.FileData;
import com.miguelbcr.ui.rx_paparazzo2.entities.Response;
import com.miguelbcr.ui.rx_paparazzo2.entities.size.CustomMaxSize;
Expand All @@ -35,6 +37,7 @@ public class SampleActivity extends AppCompatActivity implements Testable {
private RecyclerView recyclerView;
private ArrayList<FileData> fileDataList;
private Size size;
private boolean isFrontCameraMode = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -66,15 +69,15 @@ protected void onSaveInstanceState(Bundle outState) {
}

private void configureToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.app_name);
}

private void initViews() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView = (RecyclerView) findViewById(R.id.rv_images);
recyclerView = findViewById(R.id.rv_images);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);

Expand All @@ -88,6 +91,8 @@ private void initViews() {
.setOnClickListener(v -> pickupMultipleTypesFile());
findViewById(R.id.fab_pickup_multiple_types_files)
.setOnClickListener(v -> pickupMultipleTypesFiles());
((Switch)findViewById(R.id.switch_camera_mode))
.setOnCheckedChangeListener((buttonView, isChecked) -> switchCameraMode(isChecked));

loadImages();
}
Expand All @@ -96,6 +101,7 @@ private void captureImage() {
CustomMaxSize size = new CustomMaxSize(512);

Observable<Response<SampleActivity, FileData>> takeOnePhoto = pickSingle(null, size)
.setCameraMode(isFrontCameraMode ? CameraMode.FRONT : CameraMode.BACK)
.usingCamera();

processSingle(takeOnePhoto);
Expand All @@ -108,6 +114,7 @@ private void captureImageWithCrop() {

OriginalSize size = new OriginalSize();
Observable<Response<SampleActivity, FileData>> takePhotoAndCrop = pickSingle(options, size)
.setCameraMode(isFrontCameraMode ? CameraMode.FRONT : CameraMode.BACK)
.usingCamera();

processSingle(takePhotoAndCrop);
Expand Down Expand Up @@ -252,6 +259,10 @@ private void loadImages() {
recyclerView.setAdapter(new ImagesAdapter(fileDataList));
}

private void switchCameraMode(boolean isChecked) {
this.isFrontCameraMode = isChecked;
}

@Override
public List<FileData> getFileDatas() {
return fileDataList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.Toast;

import com.miguelbcr.ui.rx_paparazzo2.RxPaparazzo;
import com.miguelbcr.ui.rx_paparazzo2.entities.CameraMode;
import com.miguelbcr.ui.rx_paparazzo2.entities.FileData;
import com.miguelbcr.ui.rx_paparazzo2.entities.Response;
import com.miguelbcr.ui.rx_paparazzo2.entities.size.CustomMaxSize;
Expand All @@ -37,6 +39,7 @@ public class SampleFragment extends Fragment implements Testable {
private RecyclerView recyclerView;
private ArrayList<FileData> fileDataList;
private Size size;
private boolean isFrontCameraMode = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Expand Down Expand Up @@ -72,7 +75,7 @@ private void initViews() {

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView = (RecyclerView) view.findViewById(R.id.rv_images);
recyclerView = view.findViewById(R.id.rv_images);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);

Expand All @@ -86,6 +89,8 @@ private void initViews() {
.setOnClickListener(v -> pickupMultipleTypesFile());
view.findViewById(R.id.fab_pickup_multiple_types_files)
.setOnClickListener(v -> pickupMultipleTypesFiles());
((Switch)view.findViewById(R.id.switch_camera_mode))
.setOnCheckedChangeListener((buttonView, isChecked) -> switchCameraMode(isChecked));

loadImages();
}
Expand All @@ -94,6 +99,7 @@ private void captureImage() {
SmallSize size = new SmallSize();

Observable<Response<SampleFragment, FileData>> takeOnePhoto = pickSingle(null, size)
.setCameraMode(isFrontCameraMode ? CameraMode.FRONT : CameraMode.BACK)
.usingCamera();

processSingle(takeOnePhoto);
Expand All @@ -108,6 +114,7 @@ private void captureImageWithCrop() {
OriginalSize size = new OriginalSize();

Observable<Response<SampleFragment, FileData>> takePhotoAndCrop = pickSingle(options, size)
.setCameraMode(isFrontCameraMode ? CameraMode.FRONT : CameraMode.BACK)
.usingCamera();

processSingle(takePhotoAndCrop);
Expand Down Expand Up @@ -251,6 +258,10 @@ private void loadImages(List<FileData> fileDataList) {
recyclerView.setAdapter(new ImagesAdapter(fileDataList));
}

private void switchCameraMode(boolean isChecked) {
this.isFrontCameraMode = isChecked;
}

@Override
public List<FileData> getFileDatas() {
return fileDataList;
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/sample_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@
android:layout_weight="1"/>
</LinearLayout>

<Switch
android:id="@+id/switch_camera_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchPadding="10dp"
android:padding="10dp"
android:text="Camera Front"/>

</LinearLayout>

<android.support.v7.widget.RecyclerView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.app.Application;
import android.support.v4.app.Fragment;

import com.miguelbcr.ui.rx_paparazzo2.entities.CameraMode;
import com.miguelbcr.ui.rx_paparazzo2.entities.Config;
import com.miguelbcr.ui.rx_paparazzo2.entities.FileData;
import com.miguelbcr.ui.rx_paparazzo2.entities.Response;
Expand Down Expand Up @@ -195,6 +196,11 @@ public B useDocumentPicker() {
return self;
}

public B setCameraMode(CameraMode cameraMode) {
this.config.setCameraMode(cameraMode);
return self;
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.miguelbcr.ui.rx_paparazzo2.entities;

public enum CameraMode {

FRONT,

BACK

}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Maybe without spaces?

public enum CameraMode {
    FRONT,
    BACK
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Config {

private String fileProviderAuthority;
private String fileProviderDirectory;
private CameraMode cameraMode;

public Config() {
this.size = new ScreenSize();
Expand All @@ -58,6 +59,7 @@ public Config() {
this.fileProviderAuthority = null;
this.fileProviderDirectory = null;
this.maximumFileSize = NO_FILESIZE_LIMIT;
this.cameraMode = CameraMode.BACK;
}

public void setMaximumFileSize(long maximumFileSize) {
Expand Down Expand Up @@ -182,4 +184,11 @@ public String getFileProviderDirectory() {
return fileProviderDirectory;
}

public CameraMode getCameraMode() {
return cameraMode;
}

public void setCameraMode(CameraMode cameraMode) {
this.cameraMode = cameraMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import com.miguelbcr.ui.rx_paparazzo2.entities.CameraMode;
import com.miguelbcr.ui.rx_paparazzo2.entities.Config;
import com.miguelbcr.ui.rx_paparazzo2.entities.FileData;
import com.miguelbcr.ui.rx_paparazzo2.entities.TargetUi;
Expand Down Expand Up @@ -69,7 +71,7 @@ public FileData apply(Uri uri) throws Exception {

private Function<Intent, Uri> revokeFileReadWritePermissions(final TargetUi targetUi, final Uri uri) {
return new Function<Intent, Uri>() {
@Override public Uri apply(Intent data) throws Exception {
@Override public Uri apply(Intent data) {
PermissionUtil.revokeFileReadWritePermissions(targetUi, uri);

return uri;
Expand All @@ -94,6 +96,16 @@ private Intent getIntentCamera(Uri uri) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

if (CameraMode.FRONT.equals(config.getCameraMode())) {
intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that android.hardware.Camera.CameraInfo is deprecated from API 21 (Build.VERSION_CODES.LOLLIPOP) -> https://developer.android.com/reference/android/hardware/Camera.CameraInfo

Instead, suggests to use android.hardware.camera2. -> https://developer.android.com/reference/android/hardware/camera2/package-summary

I can see that LENS_FACING_FRONT was added from API 21:

I wonder if you have tested it in several android versions just to make sure it works, for instance, API 18, 21, 22, 27

I'll try it too...

} else {
intent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_BACK);
intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 0);
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", false);
}

return PermissionUtil.requestReadWritePermission(targetUi, intent, uri);
}

Expand Down