Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
memory and performance improvements for camera and proofmode
Browse files Browse the repository at this point in the history
  • Loading branch information
iamironrabbit committed Jun 29, 2018
1 parent 45ae7e0 commit 828f21c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 50 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ dependencies {

//built in camera now!
//https://github.com/siralam/CameraViewPlus
implementation 'com.asksira.android:cameraviewplus:0.9.0'
implementation 'com.asksira.android:cameraviewplus:0.9.4'

//from here: https://github.com/guardianproject/proofmode
implementation 'org.witness:android-libproofmode:0.0.2'
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/ic_launcher_name"
android:largeHeap="false"
android:theme="@style/AppTheme"
tools:replace="android:icon,android:allowBackup,android:label">
<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.android.cameraview.CameraView;
import com.google.android.cameraview.CameraViewImpl;

import org.apache.commons.io.IOUtils;
import org.awesomeapp.messenger.ImApp;
import org.awesomeapp.messenger.Preferences;
import org.awesomeapp.messenger.provider.Imps;
Expand All @@ -28,6 +29,7 @@
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.Executor;
Expand All @@ -36,7 +38,8 @@
import java.util.concurrent.TimeUnit;

import im.zom.messenger.R;

import info.guardianproject.iocipher.File;
import info.guardianproject.iocipher.FileOutputStream;


public class CameraActivity extends AppCompatActivity {
Expand Down Expand Up @@ -84,27 +87,6 @@ protected void onCreate(Bundle savedInstanceState) {

mCameraView = findViewById(R.id.camera_view);

mCameraView.setOnPictureTakenListener(new CameraViewImpl.OnPictureTakenListener() {
@Override
public void onPictureTaken(final Bitmap bitmap, final int rotationDegrees) {

mExec.execute(new Runnable()
{
public void run ()
{

if (mOneAndDone)
mHandler.sendEmptyMessage(2);

storeBitmap(rotate(bitmap,rotationDegrees));

}
});

mHandler.sendEmptyMessage(1);
}
});

findViewById(R.id.btnCamera).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -167,6 +149,7 @@ public void onOrientationChanged(int orientation) {
protected void onDestroy() {
super.onDestroy();
mOrientationEventListener.disable();
System.gc();
}

@Override
Expand Down Expand Up @@ -226,16 +209,27 @@ private void showSystemUI() {
protected void onResume() {
super.onResume();
mCameraView.start();
/**
if (isStoragePermissionGranted() && isCameraPermissionGranted()) {
mCameraView.start();
} else {
if (!isCameraPermissionGranted()) {
checkCameraPermission();
} else {
checkStoragePermission();
mCameraView.setOnPictureTakenListener(new CameraViewImpl.OnPictureTakenListener() {
@Override
public void onPictureTaken(final Bitmap bitmap, final int rotationDegrees) {

mExec.execute(new Runnable()
{
public void run ()
{

if (mOneAndDone)
mHandler.sendEmptyMessage(2);

storeBitmap(rotate(bitmap,rotationDegrees));

}
});

mHandler.sendEmptyMessage(1);
}
}**/
});

}

@Override
Expand All @@ -254,14 +248,15 @@ private void storeBitmap (Bitmap bitmap)
String offerId = UUID.randomUUID().toString();

try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();

bitmap = getResizedBitmap(bitmap,SecureMediaStore.DEFAULT_IMAGE_WIDTH,SecureMediaStore.DEFAULT_IMAGE_WIDTH);
final Uri vfsUri = SecureMediaStore.createContentPath(sessionId,"cam" + new Date().getTime() + ".jpg");

bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /*ignored for JPG*/, bos);
ByteArrayInputStream bs = new ByteArrayInputStream(bos.toByteArray());
OutputStream out = new FileOutputStream(new File(vfsUri.getPath()));
bitmap = getResizedBitmap(bitmap,SecureMediaStore.DEFAULT_IMAGE_WIDTH,SecureMediaStore.DEFAULT_IMAGE_WIDTH);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /*ignored for JPG*/, out);

final Uri vfsUri = SecureMediaStore.importContent(sessionId,"cam" + new Date().getTime() + ".jpg",bs);
bitmap.recycle();
System.gc();

String mimeType = "image/jpeg";

Expand All @@ -280,19 +275,14 @@ private void storeBitmap (Bitmap bitmap)

if (Preferences.useProofMode()) {

mExec.execute(new Runnable ()
{
public void run ()
{
try {
ProofMode.generateProof(CameraActivity.this, vfsUri);
} catch (FileNotFoundException e) {
Log.e(ImApp.LOG_TAG,"error generating proof for photo",e);
}
}
});
try {
ProofMode.generateProof(CameraActivity.this, vfsUri);
} catch (FileNotFoundException e) {
Log.e(ImApp.LOG_TAG,"error generating proof for photo",e);
}
}


}
catch (IOException ioe)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ public static Uri importContent(String sessionId, String fileName, InputStream s
return vfsUri(targetPath);
}

/**
* Copy device content into vfs.
* All imported content is stored under /SESSION_NAME/
* The original full path is retained to facilitate browsing
* The session content can be deleted when the session is over
* @param sessionId
* @return vfs uri
* @throws IOException
*/
public static Uri createContentPath(String sessionId, String fileName) throws IOException {
//list("/");
String targetPath = "/" + sessionId + "/upload/" + UUID.randomUUID().toString() + '/' + fileName.replaceAll("[^a-zA-Z0-9\\.\\-]", "_");
targetPath = createUniqueFilename(targetPath);
mkdirs( targetPath );

//list("/");
return vfsUri(targetPath);
}

/**
* Resize an image to an efficient size for sending via OTRDATA, then copy
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/layout/activity_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.cameraview.CameraView
android:id="@+id/camera_view"
android:layout_width="match_parent"
Expand All @@ -17,11 +18,13 @@
android:adjustViewBounds="true"
app:autoFocus="true"
app:cameraAspectRatio="4:3"
app:maximumWidth="1280"
app:maximumPreviewWidth="1280"
app:useHighResPicture="false"
app:facing="back"
app:flash="off"/>



<LinearLayout
android:id="@+id/btn_holder"
android:layout_width="match_parent"
Expand Down

0 comments on commit 828f21c

Please sign in to comment.