Skip to content

Commit

Permalink
Fix aspect ratio of taken pictures
Browse files Browse the repository at this point in the history
Fixes google#127
Fixes google#78
Fixes google#46
Fixes google#20

Change-Id: I93f62823f3c1fd8a28ee4b659a9a5b7c81f183fc
  • Loading branch information
yaraki committed Apr 13, 2017
1 parent 0c87b4e commit 9bbe07b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.google.android.cameraview.AspectRatio;

import java.util.Arrays;
import java.util.Set;


Expand Down Expand Up @@ -75,6 +76,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
if (ratios == null) {
throw new RuntimeException("No ratios");
}
Arrays.sort(ratios);
final AspectRatio current = args.getParcelable(ARG_CURRENT_ASPECT_RATIO);
final AspectRatioAdapter adapter = new AspectRatioAdapter(ratios, current);
return new AlertDialog.Builder(getActivity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
Expand Down Expand Up @@ -188,30 +189,32 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.aspect_ratio:
if (mCameraView != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (mCameraView != null
&& fragmentManager.findFragmentByTag(FRAGMENT_DIALOG) == null) {
final Set<AspectRatio> ratios = mCameraView.getSupportedAspectRatios();
final AspectRatio currentRatio = mCameraView.getAspectRatio();
AspectRatioFragment.newInstance(ratios, currentRatio)
.show(getSupportFragmentManager(), FRAGMENT_DIALOG);
.show(fragmentManager, FRAGMENT_DIALOG);
}
break;
return true;
case R.id.switch_flash:
if (mCameraView != null) {
mCurrentFlash = (mCurrentFlash + 1) % FLASH_OPTIONS.length;
item.setTitle(FLASH_TITLES[mCurrentFlash]);
item.setIcon(FLASH_ICONS[mCurrentFlash]);
mCameraView.setFlash(FLASH_OPTIONS[mCurrentFlash]);
}
break;
return true;
case R.id.switch_camera:
if (mCameraView != null) {
int facing = mCameraView.getFacing();
mCameraView.setFacing(facing == CameraView.FACING_FRONT ?
CameraView.FACING_BACK : CameraView.FACING_FRONT);
}
break;
return true;
}
return false;
return super.onOptionsItemSelected(item);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_camera"
app:useCompatPadding="true"
tools:ignore="RelativeOverlap"/>

</RelativeLayout>
4 changes: 2 additions & 2 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_camera"/>
app:srcCompat="@drawable/ic_camera"
app:useCompatPadding="true"/>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ boolean setAspectRatio(AspectRatio ratio) {
return false;
}
mAspectRatio = ratio;
prepareImageReader();
if (mCaptureSession != null) {
mCaptureSession.close();
mCaptureSession = null;
Expand Down Expand Up @@ -443,6 +444,9 @@ protected void collectPictureSizes(SizeMap sizes, StreamConfigurationMap map) {
}

private void prepareImageReader() {
if (mImageReader != null) {
mImageReader.close();
}
Size largest = mPictureSizes.sizes(mAspectRatio).last();
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
ImageFormat.JPEG, /* maxImages */ 2);
Expand Down

0 comments on commit 9bbe07b

Please sign in to comment.