Skip to content
Snippets Groups Projects
Commit f4e95d70 authored by Ghazi Akmal Fauzan's avatar Ghazi Akmal Fauzan
Browse files

feat: access camera preview

parent 07d4b550
No related merge requests found
...@@ -53,6 +53,10 @@ dependencies { ...@@ -53,6 +53,10 @@ dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4") implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.google.android.gms:play-services-maps:18.2.0") implementation("com.google.android.gms:play-services-maps:18.2.0")
implementation("com.google.android.gms:play-services-location:21.2.0") implementation("com.google.android.gms:play-services-location:21.2.0")
implementation("androidx.camera:camera-core:1.3.2")
implementation("androidx.camera:camera-camera2:1.3.2")
implementation("androidx.camera:camera-lifecycle:1.3.2")
implementation("androidx.camera:camera-view:1.3.2")
testImplementation("junit:junit:4.13.2") testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
......
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera.any" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_primary_logo" android:icon="@mipmap/ic_primary_logo"
......
...@@ -5,56 +5,64 @@ import androidx.fragment.app.Fragment ...@@ -5,56 +5,64 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView
import androidx.core.content.ContextCompat
import com.example.nerbos.R import com.example.nerbos.R
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [ScanFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ScanFragment : Fragment() { class ScanFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null private lateinit var previewView: PreviewView
private var param2: String? = null private lateinit var cameraExecutor: ExecutorService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
// Inflate the layout for this fragment // Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_scan, container, false) val view = inflater.inflate(R.layout.fragment_scan, container, false)
previewView = view.findViewById(R.id.previewView)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
cameraExecutor = Executors.newSingleThreadExecutor()
startCamera()
} }
companion object { private fun startCamera() {
/** val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
* Use this factory method to create a new instance of
* this fragment using the provided parameters. cameraProviderFuture.addListener({
* val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
* @param param1 Parameter 1.
* @param param2 Parameter 2. val preview = Preview.Builder()
* @return A new instance of fragment Scan. .build()
*/ .also {
// TODO: Rename and change types and number of parameters it.setSurfaceProvider(previewView.surfaceProvider)
@JvmStatic
fun newInstance(param1: String, param2: String) =
ScanFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
} }
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(this, cameraSelector, preview)
} catch (e: Exception) {
e.printStackTrace()
} }
}, ContextCompat.getMainExecutor(requireContext()))
}
override fun onDestroy() {
super.onDestroy()
cameraExecutor.shutdown()
} }
} }
\ No newline at end of file
...@@ -3,14 +3,59 @@ ...@@ -3,14 +3,59 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/primary_bg"
tools:context=".fragments.scan.ScanFragment"> tools:context=".fragments.scan.ScanFragment">
<!-- TODO: Update blank fragment layout --> <LinearLayout
<TextView android:id="@+id/scanVerticalLayout"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:text="Scan Fragment" android:orientation="vertical">
android:textSize="26dp"
android:layout_gravity="center" /> <Space
android:layout_width="match_parent"
android:layout_height="75dp" />
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="300dp"
android:layout_height="400dp"
android:layout_gravity="center" />
<LinearLayout
android:id="@+id/scanHorizontalLayout"
android:layout_width="300dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/captureButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/capture_button"
android:src="@android:drawable/ic_menu_camera" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton
android:id="@+id/galleryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/gallery_button"
android:src="@android:drawable/ic_menu_gallery" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="75dp" />
</LinearLayout>
</FrameLayout> </FrameLayout>
\ No newline at end of file
...@@ -51,5 +51,7 @@ ...@@ -51,5 +51,7 @@
<string name="network_sensing_string">No Internet Connection Please check your connection and try again.</string> <string name="network_sensing_string">No Internet Connection Please check your connection and try again.</string>
<string name="close_button">Close Button</string> <string name="close_button">Close Button</string>
<string name="pie_chart">Pie Chart</string> <string name="pie_chart">Pie Chart</string>
<string name="capture_button">Capture Button</string>
<string name="gallery_button">Gallery Button</string>
</resources> </resources>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment