Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.appsthatmatter:android-motion-detection:'
}
dependencies {
implementation("com.github.appsthatmatter:android-motion-detection:")
}
<dependency>
<groupId>com.github.appsthatmatter</groupId>
<artifactId>android-motion-detection</artifactId>
<version></version>
</dependency>
libraryDependencies += "com.github.appsthatmatter" % "android-motion-detection" % ""
:dependencies [[com.github.appsthatmatter/android-motion-detection ""]]
The preview of the camera will be drawn on a 1x1 pixel surface. At this point the preview input of the camera will be analysed at a given interval. To detect a movement the picture will be dived into tiles. For each tile the average brightness will be calculated. If a average value of a single tile differs from the previous value, a motion is registered.
motiondetection
into your project. Adapt the package names.SurfaceView
in your layout:<SurfaceView
android:layout_width="1dp"
android:layout_height="1dp"
android:id="@+id/surfaceView" />
MotionDetector
motionDetector = new MotionDetector(this, (SurfaceView) findViewById(R.id.surfaceView));
motionDetector.onResume() and .onPause()
motionDetector.setMotionDetectorCallback(new MotionDetectorCallback() {
@Override
public void onMotionDetected() {
txtStatus.setText("Bewegung erkannt");
}
@Override
public void onTooDark() {
txtStatus.setText("Zu dunkel hier");
}
});
there are some important parameters that can be adjusted at runtime.
| Method | Description | Default | | --- | --- | --- | | motionDetector.setCheckInterval(500); | milliseconds between pictures to compare. less = less energy cos t | 500 | | motionDetector.setLeniency(20); | maximal tolerence of difference in pictures. less = more sensible | 20 | | motionDetector.setMinLuma(1000); | minimum brightness. if lower the callback onTooDark is called | 1000 |
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA"/>
targetSdkVersion
is greater than 21 you have to implement Runtime PermissionsJonas Gehring Apache license 2.0