Skip to content

Commit 6dc57c6

Browse files
committed
chore(android): project to natively develop the lib
1 parent 1de1d76 commit 6dc57c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1059
-4
lines changed

src-native/android/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

3+
subprojects {ext.cameraxVersion = "1.4.0-alpha02"}
4+
35
buildscript {
46
ext.kotlin_version = '1.8.0'
57
repositories {
68
google()
79
mavenCentral()
810
}
911
dependencies {
10-
classpath 'com.android.tools.build:gradle:7.3.1'
12+
classpath 'com.android.tools.build:gradle:8.2.0-rc02'
1113
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1214
}
1315
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
android {
6+
namespace 'com.nativescript.camerademoapp'
7+
compileSdk 34
8+
9+
defaultConfig {
10+
applicationId "com.nativescript.camerademoapp"
11+
minSdk 24
12+
targetSdk 34
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
kotlinOptions {
30+
jvmTarget = '1.8'
31+
}
32+
buildFeatures {
33+
viewBinding true
34+
}
35+
}
36+
37+
dependencies {
38+
39+
implementation 'androidx.core:core-ktx:1.12.0'
40+
implementation 'androidx.appcompat:appcompat:1.6.1'
41+
implementation 'com.google.android.material:material:1.10.0'
42+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
43+
implementation 'androidx.navigation:navigation-fragment-ktx:2.7.5'
44+
implementation 'androidx.navigation:navigation-ui-ktx:2.7.5'
45+
implementation project(':ui-camera')
46+
testImplementation 'junit:junit:4.13.2'
47+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
48+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
49+
50+
def cameraxVersion = project.hasProperty("cameraxVersion") ? project.cameraxVersion : "1.2.3"
51+
implementation "androidx.camera:camera-core:${cameraxVersion}"
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.nativescript.camerademoapp
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.nativescript.camerademoapp", appContext.packageName)
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<uses-feature
5+
android:name="android.hardware.camera"
6+
android:required="true" />
7+
<uses-permission android:name="android.permission.CAMERA"/>
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/Theme.Android">
15+
<activity
16+
android:name=".MainActivity"
17+
android:exported="true"
18+
android:label="@string/app_name"
19+
android:theme="@style/Theme.Android">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
27+
</application>
28+
29+
</manifest>
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.nativescript.camerademoapp
2+
3+
import android.content.pm.PackageManager
4+
import android.graphics.Color
5+
import android.media.Image
6+
import android.os.Bundle
7+
import android.util.Log
8+
import androidx.fragment.app.Fragment
9+
import android.view.LayoutInflater
10+
import android.view.View
11+
import android.view.ViewGroup
12+
import androidx.activity.result.contract.ActivityResultContracts
13+
import androidx.core.app.ActivityCompat
14+
import androidx.core.content.ContextCompat
15+
import androidx.navigation.fragment.findNavController
16+
import com.google.android.material.snackbar.Snackbar
17+
import com.nativescript.camerademoapp.databinding.FragmentFirstBinding
18+
import com.nativescript.cameraview.CameraEventListener
19+
import com.nativescript.cameraview.ImageAnalysis
20+
import com.nativescript.cameraview.ImageAsyncProcessor
21+
import java.io.File
22+
import java.lang.Exception
23+
24+
/**
25+
* A simple [Fragment] subclass as the default destination in the navigation.
26+
*/
27+
class FirstFragment : Fragment() {
28+
29+
private var photoTime: Long = 0
30+
private var _binding: FragmentFirstBinding? = null
31+
32+
// This property is only valid between onCreateView and
33+
// onDestroyView.
34+
private val binding get() = _binding!!
35+
36+
private val requestPermissionLauncher =
37+
registerForActivityResult(
38+
ActivityResultContracts.RequestPermission()
39+
) { isGranted: Boolean ->
40+
if (isGranted) {
41+
binding.cameraView.startPreview()
42+
Log.i("Permission: ", "Granted")
43+
} else {
44+
Log.i("Permission: ", "Denied")
45+
}
46+
}
47+
48+
49+
override fun onCreateView(
50+
inflater: LayoutInflater, container: ViewGroup?,
51+
savedInstanceState: Bundle?
52+
): View? {
53+
54+
_binding = FragmentFirstBinding.inflate(inflater, container, false)
55+
return binding.root
56+
57+
}
58+
59+
fun requestCameraPermission() {
60+
when {
61+
ContextCompat.checkSelfPermission(
62+
this.requireActivity(),
63+
android.Manifest.permission.CAMERA
64+
) == PackageManager.PERMISSION_GRANTED -> {
65+
// layout.showSnackbar(
66+
// view,
67+
// "granted",
68+
// Snackbar.LENGTH_INDEFINITE,
69+
// null
70+
// ) {}
71+
}
72+
73+
ActivityCompat.shouldShowRequestPermissionRationale(
74+
this.requireActivity(),
75+
android.Manifest.permission.CAMERA
76+
) -> {
77+
view?.let {
78+
Snackbar.make(it, "permission required", Snackbar.LENGTH_INDEFINITE)
79+
.setAction("ok") {
80+
requestPermissionLauncher.launch(
81+
android.Manifest.permission.CAMERA
82+
)
83+
}
84+
.show()
85+
}
86+
}
87+
88+
else -> {
89+
requestPermissionLauncher.launch(
90+
android.Manifest.permission.CAMERA
91+
)
92+
}
93+
}
94+
95+
}
96+
97+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
98+
super.onViewCreated(view, savedInstanceState)
99+
binding.cameraView.listener = object : CameraEventListener {
100+
override fun onReady() {
101+
Log.d("CameraView","onReady")
102+
}
103+
104+
override fun onCameraOpen() {
105+
Log.d("CameraView","onCameraOpen")
106+
}
107+
108+
override fun onCameraClose() {
109+
Log.d("CameraView","onCameraClose")
110+
}
111+
112+
override fun onCameraPhoto(file: File?) {
113+
Log.d("CameraView","onCameraPhoto: " + ((System.nanoTime()-photoTime)/1000000) + "ms" )
114+
}
115+
116+
override fun onCameraPhotoImage(
117+
image: Image?,
118+
info: androidx.camera.core.ImageInfo,
119+
processor: ImageAsyncProcessor
120+
) {
121+
Log.d("CameraView","onCameraPhotoImage: " + ((System.nanoTime()-photoTime)/1000000) + "ms" )
122+
processor.finished()
123+
}
124+
125+
override fun onCameraVideo(file: File?) {
126+
Log.d("CameraView","onCameraVideo")
127+
}
128+
129+
override fun onCameraAnalysis(analysis: ImageAnalysis) {
130+
Log.d("CameraView","onCameraAnalysis")
131+
}
132+
133+
override fun onCameraError(message: String, ex: Exception) {
134+
Log.d("CameraView","onCameraError")
135+
}
136+
137+
override fun onCameraVideoStart() {
138+
Log.d("CameraView","onCameraVideoStart")
139+
}
140+
141+
}
142+
requestCameraPermission()
143+
// binding.cameraView.startPreview()
144+
// binding.buttonFirst.setOnClickListener {
145+
// findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
146+
// }
147+
148+
binding.fab.setOnClickListener { view ->
149+
photoTime = System.nanoTime()
150+
binding.cameraView.takePhoto()
151+
}
152+
}
153+
154+
override fun onDestroyView() {
155+
super.onDestroyView()
156+
_binding = null
157+
}
158+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.nativescript.camerademoapp
2+
3+
import android.os.Bundle
4+
import com.google.android.material.snackbar.Snackbar
5+
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.navigation.findNavController
7+
import androidx.navigation.ui.AppBarConfiguration
8+
import androidx.navigation.ui.navigateUp
9+
import androidx.navigation.ui.setupActionBarWithNavController
10+
import android.view.Menu
11+
import android.view.MenuItem
12+
import com.nativescript.camerademoapp.databinding.ActivityMainBinding
13+
14+
class MainActivity : AppCompatActivity() {
15+
16+
private lateinit var appBarConfiguration: AppBarConfiguration
17+
private lateinit var binding: ActivityMainBinding
18+
19+
override fun onCreate(savedInstanceState: Bundle?) {
20+
super.onCreate(savedInstanceState)
21+
22+
binding = ActivityMainBinding.inflate(layoutInflater)
23+
setContentView(binding.root)
24+
25+
setSupportActionBar(binding.toolbar)
26+
27+
val navController = findNavController(R.id.nav_host_fragment_content_main)
28+
appBarConfiguration = AppBarConfiguration(navController.graph)
29+
setupActionBarWithNavController(navController, appBarConfiguration)
30+
}
31+
32+
override fun onCreateOptionsMenu(menu: Menu): Boolean {
33+
// Inflate the menu; this adds items to the action bar if it is present.
34+
menuInflater.inflate(R.menu.menu_main, menu)
35+
return true
36+
}
37+
38+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
39+
// Handle action bar item clicks here. The action bar will
40+
// automatically handle clicks on the Home/Up button, so long
41+
// as you specify a parent activity in AndroidManifest.xml.
42+
return when (item.itemId) {
43+
R.id.action_settings -> true
44+
else -> super.onOptionsItemSelected(item)
45+
}
46+
}
47+
48+
override fun onSupportNavigateUp(): Boolean {
49+
val navController = findNavController(R.id.nav_host_fragment_content_main)
50+
return navController.navigateUp(appBarConfiguration)
51+
|| super.onSupportNavigateUp()
52+
}
53+
}

0 commit comments

Comments
 (0)