update cam libs to sdk=30 versions, improve previews and removal of temp images in case of cancellations

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2021-09-16 17:55:18 +02:00
parent 67c421a067
commit 7e688c43af
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
4 changed files with 75 additions and 23 deletions

View File

@ -189,7 +189,7 @@ dependencies {
implementation 'androidx.camera:camera-camera2:1.0.1'
implementation 'androidx.camera:camera-lifecycle:1.0.1'
implementation 'androidx.camera:camera-view:1.0.0-alpha20'
implementation 'androidx.camera:camera-view:1.0.0-alpha28'
implementation "androidx.exifinterface:exifinterface:1.3.3"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

View File

@ -28,8 +28,8 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Size;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.View;
@ -39,11 +39,10 @@ import com.google.common.util.concurrent.ListenableFuture;
import com.nextcloud.talk.R;
import com.nextcloud.talk.databinding.ActivityTakePictureBinding;
import com.nextcloud.talk.models.TakePictureViewModel;
import com.nextcloud.talk.utils.BitmapShrinker;
import com.nextcloud.talk.utils.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
@ -65,6 +64,9 @@ public class TakePhotoActivity extends AppCompatActivity {
private static final String TAG = TakePhotoActivity.class.getSimpleName();
private static final float MAX_SCALE = 6.0f;
private static final float MEDIUM_SCALE = 2.45f;
private ActivityTakePictureBinding binding;
private TakePictureViewModel viewModel;
@ -124,13 +126,20 @@ public class TakePhotoActivity extends AppCompatActivity {
Log.w(TAG, "Error deleting temp camera image");
}
binding.takePhoto.setEnabled(true);
binding.photoPreview.setTag(null);
showCameraElements();
});
binding.send.setOnClickListener((v) -> {
Uri uri = (Uri) binding.photoPreview.getTag();
setResult(RESULT_OK, new Intent().setDataAndType(uri, "image/jpeg"));
binding.photoPreview.setTag(null);
finish();
});
// Enable enlarging the image more than default 3x maximumScale.
// Medium scale adapted to make double-tap behaviour more consistent.
binding.photoPreview.setMaximumScale(MAX_SCALE);
binding.photoPreview.setMediumScale(MEDIUM_SCALE);
} catch (IllegalArgumentException | ExecutionException | InterruptedException e) {
Log.e(TAG, "Error taking picture", e);
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
@ -139,10 +148,25 @@ public class TakePhotoActivity extends AppCompatActivity {
}, ContextCompat.getMainExecutor(this));
}
@Override
public void onBackPressed() {
Uri uri = (Uri) binding.photoPreview.getTag();
if (uri != null) {
File photoFile = new File(uri.getPath());
if (!photoFile.delete()) {
Log.w(TAG, "Error deleting temp camera image");
}
binding.photoPreview.setTag(null);
}
super.onBackPressed();
}
private void showCameraElements() {
binding.send.setVisibility(View.GONE);
binding.retake.setVisibility(View.GONE);
binding.photoPreview.setVisibility(View.GONE);
binding.photoPreview.setVisibility(View.INVISIBLE);
binding.preview.setVisibility(View.VISIBLE);
binding.takePhoto.setVisibility(View.VISIBLE);
@ -151,7 +175,7 @@ public class TakePhotoActivity extends AppCompatActivity {
}
private void showPictureProcessingElements() {
binding.preview.setVisibility(View.GONE);
binding.preview.setVisibility(View.INVISIBLE);
binding.takePhoto.setVisibility(View.GONE);
binding.switchCamera.setVisibility(View.GONE);
binding.toggleTorch.setVisibility(View.GONE);
@ -162,7 +186,7 @@ public class TakePhotoActivity extends AppCompatActivity {
}
private ImageCapture getImageCapture() {
final ImageCapture imageCapture = new ImageCapture.Builder().setTargetResolution(new Size(720, 1280)).build();
final ImageCapture imageCapture = new ImageCapture.Builder().build();
orientationEventListener = new OrientationEventListener(this) {
@Override
@ -199,19 +223,8 @@ public class TakePhotoActivity extends AppCompatActivity {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
final Uri savedUri = Uri.fromFile(photoFile);
Log.i(TAG, "onImageSaved - savedUri:" + savedUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
try {
binding.photoPreview.setImageBitmap(
BitmapFactory.decodeStream(new FileInputStream(photoFile), null, options));
binding.photoPreview.setRotation(getImageOrientation(photoFile));
binding.photoPreview.setTag(savedUri);
showPictureProcessingElements();
} catch (FileNotFoundException e) {
Log.w(TAG, "Error reading image", e);
}
setPreviewImage(photoFile);
showPictureProcessingElements();
}
@Override
@ -232,6 +245,22 @@ public class TakePhotoActivity extends AppCompatActivity {
return imageCapture;
}
private void setPreviewImage(File photoFile) {
final Uri savedUri = Uri.fromFile(photoFile);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int doubleScreenWidth = displayMetrics.widthPixels * 2;
int doubleScreenHeight = displayMetrics.heightPixels * 2;
Bitmap bitmap = BitmapShrinker.shrinkBitmap(photoFile.getAbsolutePath(),
doubleScreenWidth,
doubleScreenHeight);
binding.photoPreview.setImageBitmap(bitmap);
binding.photoPreview.setTag(savedUri);
}
public int getImageOrientation(File imageFile) {
int rotate = 0;
try {
@ -284,6 +313,28 @@ public class TakePhotoActivity extends AppCompatActivity {
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
if (binding.photoPreview.getTag() != null) {
savedInstanceState.putString("Uri", ((Uri) binding.photoPreview.getTag()).getPath());
}
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
String uri = savedInstanceState.getString("Uri", null);
if (uri != null) {
File photoFile = new File(uri);
setPreviewImage(photoFile);
showPictureProcessingElements();
}
}
public static Intent createIntent(@NonNull Context context) {
return new Intent(context, TakePhotoActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
}

View File

@ -34,6 +34,7 @@ object BitmapShrinker {
private const val DEGREES_180 = 180f
private const val DEGREES_270 = 270f
@JvmStatic
fun shrinkBitmap(
path: String,
reqWidth: Int,

View File

@ -26,7 +26,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:theme="@style/TransparentTheme">
<androidx.camera.view.PreviewView
@ -34,12 +34,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/photo_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:visibility="gone" />
android:visibility="invisible" />
<com.google.android.material.button.MaterialButton
android:id="@+id/toggle_torch"