Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e9a2186
initial
kkawai Mar 23, 2017
08ca696
added premise SimpleCameraActivity to library
kkawai Mar 24, 2017
69652bb
added SimpleCameraActivity to library
kkawai Mar 24, 2017
bdfbb23
added premise SimpleCameraActivity to library; fixed output
kkawai Mar 24, 2017
8c3fef8
update TAG
kkawai Mar 24, 2017
9f1dd0e
write photo to given raw file path, not File uri, which is mainly for…
kkawai Mar 24, 2017
12a7e91
touch ups
kkawai Mar 24, 2017
c43c49f
touch ups
kkawai Mar 24, 2017
4e2cac1
touch ups
kkawai Mar 27, 2017
6afd807
Merge pull request #1 from google/master
kkawai Mar 27, 2017
3c5956a
Merge branch 'master' of https://github.com/premisedata/cameraview
kkawai Mar 27, 2017
ea5812c
touch ups
kkawai Mar 27, 2017
f729f79
touch ups
kkawai Mar 27, 2017
ebfac8d
touch ups
kkawai Mar 27, 2017
a6f7a35
touch ups
kkawai Mar 27, 2017
33498eb
touch ups
kkawai Mar 27, 2017
efb88f3
touch ups
kkawai Mar 27, 2017
4cc984a
touch ups
kkawai Mar 28, 2017
885a6f8
purified back
kkawai Mar 30, 2017
c0ccf80
0.2.6 - fixed some memory leaks; fixed demo project
kkawai Apr 5, 2017
172205e
initial
kkawai Mar 23, 2017
18e862e
added premise SimpleCameraActivity to library
kkawai Mar 24, 2017
c83e06f
added SimpleCameraActivity to library
kkawai Mar 24, 2017
a403531
added premise SimpleCameraActivity to library; fixed output
kkawai Mar 24, 2017
bb6be2b
update TAG
kkawai Mar 24, 2017
593868a
write photo to given raw file path, not File uri, which is mainly for…
kkawai Mar 24, 2017
21aa401
touch ups
kkawai Mar 24, 2017
55a9b66
touch ups
kkawai Mar 24, 2017
e32ffde
touch ups
kkawai Mar 27, 2017
4a6a866
touch ups
kkawai Mar 27, 2017
f3ba066
touch ups
kkawai Mar 27, 2017
b59ad49
touch ups
kkawai Mar 27, 2017
47f4808
touch ups
kkawai Mar 27, 2017
a65dfaa
touch ups
kkawai Mar 27, 2017
c50efd9
touch ups
kkawai Mar 27, 2017
e9935e4
touch ups
kkawai Mar 28, 2017
e56b55e
purified back
kkawai Mar 30, 2017
7420796
0.2.6 - fixed some memory leaks; fixed demo project
kkawai Apr 5, 2017
3bc2368
Merge branch 'master' of https://github.com/premisedata/cameraview
kkawai Apr 7, 2017
e096d96
0.2.7 - pulled in the fix for front-facing camera; added weak referen…
kkawai Apr 12, 2017
00e8800
0.2.8 - reverted all the changes to 'demo' project back to original g…
kkawai Apr 12, 2017
17eb039
0.2.8 - updated library version
kkawai Apr 12, 2017
16f05e2
0.2.8 - simplified code to be as similar as possible to the original …
kkawai Apr 12, 2017
8214fb1
0.2.9 - fixed 1st generation nexus 7 bug
kkawai Apr 18, 2017
89f4ad4
0.2.9 - fixed 1st generation nexus 7 bug; bumped up version
kkawai Apr 18, 2017
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
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.1.0'
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you suspend these changes in build scripts?

}
}

Expand All @@ -23,7 +24,7 @@ task clean(type: Delete) {
}

ext {
buildToolsVersion = '25.0.2'
buildToolsVersion = '25.0.1'
compileSdkVersion = 25
minSdkVersion = 9
targetSdkVersion = 25
Expand Down
31 changes: 31 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

apply plugin: 'com.android.library'
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
Expand All @@ -38,6 +40,35 @@ android {
}
}

artifactory {
contextUrl = "${System.env.ARTIFACTORY_URL}"
publish {
repository {
repoKey = 'private-local'
username = "${System.env.ARTIFACTORY_USERNAME}"
password = "${System.env.ARTIFACTORY_PASSWORD}"
maven = true
}
defaults {
publications('aar')
}
}
}



publishing {
publications {
aar(MavenPublication) {
groupId 'premise.android.cameraview'
version '0.2.9'
artifactId 'cameraview'

artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
}
}
}

dependencies {
compile "com.android.support:support-annotations:$supportLibraryVersion"
compile "com.android.support:support-v4:$supportLibraryVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,13 @@ private boolean setFlashInternal(int flash) {
mCameraParameters.setFlashMode(mode);
mFlash = flash;
return true;
}
}
String currentMode = FLASH_MODES.get(mFlash);
if (modes == null || !modes.contains(currentMode)) {
/**
* kkawai, fix for 1st gen Nexus 7 that only has front facing
* camera with no flash
*/
if (modes != null && !modes.contains(currentMode)) {
mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mFlash = Constants.FLASH_OFF;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ void configureTransform() {
0.f, height, // bottom right
}, 0,
4);
} else if (mDisplayOrientation == 180) {
matrix.postRotate(180, getWidth() / 2, getHeight() / 2);
}
mTextureView.setTransform(matrix);
}
Expand Down
29 changes: 19 additions & 10 deletions library/src/main/java/com/google/android/cameraview/CameraView.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Set;

Expand Down Expand Up @@ -96,13 +97,13 @@ public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
}
// Internal setup
final PreviewImpl preview = createPreviewImpl(context);
mCallbacks = new CallbackBridge();
mCallbacks = new CallbackBridge(this);
if (Build.VERSION.SDK_INT < 21) {
mImpl = new Camera1(mCallbacks, preview);
} else if (Build.VERSION.SDK_INT < 23) {
mImpl = new Camera2(mCallbacks, preview, context);
mImpl = new Camera2(mCallbacks, preview, context.getApplicationContext());
Copy link
Author

Choose a reason for hiding this comment

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

Important for memory leak fix is to pass the application context here.

} else {
mImpl = new Camera2Api23(mCallbacks, preview, context);
mImpl = new Camera2Api23(mCallbacks, preview, context.getApplicationContext());
Copy link
Author

Choose a reason for hiding this comment

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

Important for memory leak fix is to pass the application context here.

}
// Attributes
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraView, defStyleAttr,
Expand Down Expand Up @@ -152,6 +153,9 @@ protected void onDetachedFromWindow() {
mDisplayOrientationDetector.disable();
}
super.onDetachedFromWindow();
if (mCallbacks != null && mCallbacks.mCallbacks != null) {
mCallbacks.mCallbacks.clear();
Copy link
Author

Choose a reason for hiding this comment

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

Important for memory leak: clear out any callbacks to ensure no extra references are held

}
}

@Override
Expand Down Expand Up @@ -407,13 +411,16 @@ public void takePicture() {
mImpl.takePicture();
}

private class CallbackBridge implements CameraViewImpl.Callback {
private static class CallbackBridge implements CameraViewImpl.Callback {
Copy link
Author

Choose a reason for hiding this comment

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

Important for memory leak: non-static inner classes implicitly hold references to their parent. This was causing a big leak. Making it static fixes that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you actually experience memory leaks due to this?
It seems to me CallbackBridge won't be an issue. It does hold a reference to CameraView, but it won't outlive the CameraView.

Copy link
Author

Choose a reason for hiding this comment

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

My team uses LeakCanary to detect memory leaks. Unfortunately, the warnings were not false-positives. When I implemented these simple changes, the warnings went away.
It might be helpful to integrate LeakCanary into the demo app. It only takes a few minutes and run the app a few times to find out the leaks.


private final ArrayList<Callback> mCallbacks = new ArrayList<>();

private boolean mRequestLayoutOnOpen;

CallbackBridge() {
private WeakReference<CameraView> cameraView;

CallbackBridge(CameraView cameraView) {
this.cameraView = new WeakReference<>(cameraView);
Copy link
Author

@kkawai kkawai Apr 12, 2017

Choose a reason for hiding this comment

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

Important for memory leak: Since CallbackBridge is now static, we must pass the CameraView. Within CallbackBridge only hold a weak reference to it.

}

public void add(Callback callback) {
Expand All @@ -428,24 +435,26 @@ public void remove(Callback callback) {
public void onCameraOpened() {
if (mRequestLayoutOnOpen) {
mRequestLayoutOnOpen = false;
requestLayout();
if (cameraView.get() != null) {
cameraView.get().requestLayout();
}
}
for (Callback callback : mCallbacks) {
callback.onCameraOpened(CameraView.this);
callback.onCameraOpened(cameraView.get());
}
}

@Override
public void onCameraClosed() {
for (Callback callback : mCallbacks) {
callback.onCameraClosed(CameraView.this);
callback.onCameraClosed(cameraView.get());
}
}

@Override
public void onPictureTaken(byte[] data) {
for (Callback callback : mCallbacks) {
callback.onPictureTaken(CameraView.this, data);
callback.onPictureTaken(cameraView.get(), data);
}
}

Expand Down Expand Up @@ -488,7 +497,7 @@ public void writeToParcel(Parcel out, int flags) {
out.writeInt(flash);
}

public static final Parcelable.Creator<SavedState> CREATOR
public static final Creator<SavedState> CREATOR
= ParcelableCompat.newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {

@Override
Expand Down