commit f009372732b8c0a1c72b2ebc19339ed5e0469bd0 Author: Laureηt Date: Wed Jun 21 20:25:05 2023 +0200 init diff --git a/CameraShot/.gitignore b/CameraShot/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/CameraShot/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/CameraShot/.idea/.gitignore b/CameraShot/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/CameraShot/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/CameraShot/.idea/codeStyles/Project.xml b/CameraShot/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..7643783 --- /dev/null +++ b/CameraShot/.idea/codeStyles/Project.xml @@ -0,0 +1,123 @@ + + + + + + + + + + \ No newline at end of file diff --git a/CameraShot/.idea/codeStyles/codeStyleConfig.xml b/CameraShot/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/CameraShot/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/CameraShot/.idea/compiler.xml b/CameraShot/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/CameraShot/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CameraShot/.idea/gradle.xml b/CameraShot/.idea/gradle.xml new file mode 100644 index 0000000..13c29e5 --- /dev/null +++ b/CameraShot/.idea/gradle.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/CameraShot/.idea/misc.xml b/CameraShot/.idea/misc.xml new file mode 100644 index 0000000..a64e94c --- /dev/null +++ b/CameraShot/.idea/misc.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/CameraShot/app/.gitignore b/CameraShot/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/CameraShot/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/CameraShot/app/build.gradle b/CameraShot/app/build.gradle new file mode 100644 index 0000000..7b714f3 --- /dev/null +++ b/CameraShot/app/build.gradle @@ -0,0 +1,43 @@ +plugins { + id 'com.android.application' + id 'org.jetbrains.kotlin.android' +} + +android { + compileSdk 32 + + defaultConfig { + applicationId "lfainsin.enseeiht.camerashot" + minSdk 22 + targetSdk 32 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + + implementation 'androidx.core:core-ktx:1.7.0' + implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'com.google.android.material:material:1.5.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} \ No newline at end of file diff --git a/CameraShot/app/proguard-rules.pro b/CameraShot/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/CameraShot/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/CameraShot/app/src/androidTest/java/lfainsin/enseeiht/camerashot/ExampleInstrumentedTest.kt b/CameraShot/app/src/androidTest/java/lfainsin/enseeiht/camerashot/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..0949c04 --- /dev/null +++ b/CameraShot/app/src/androidTest/java/lfainsin/enseeiht/camerashot/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package lfainsin.enseeiht.camerashot + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("lfainsin.enseeiht.camerashot", appContext.packageName) + } +} \ No newline at end of file diff --git a/CameraShot/app/src/main/AndroidManifest.xml b/CameraShot/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..31dcab7 --- /dev/null +++ b/CameraShot/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CameraShot/app/src/main/java/lfainsin/enseeiht/camerashot/CameraPreview.kt b/CameraShot/app/src/main/java/lfainsin/enseeiht/camerashot/CameraPreview.kt new file mode 100644 index 0000000..8738b24 --- /dev/null +++ b/CameraShot/app/src/main/java/lfainsin/enseeiht/camerashot/CameraPreview.kt @@ -0,0 +1,67 @@ +import android.content.ContentValues.TAG +import android.content.Context +import android.hardware.Camera +import android.util.Log +import android.view.SurfaceHolder +import android.view.SurfaceView +import java.io.IOException + +/** A basic Camera preview class */ +class CameraPreview( + context: Context, + private val mCamera: Camera +) : SurfaceView(context), SurfaceHolder.Callback { + + private val mHolder: SurfaceHolder = holder.apply { + // Install a SurfaceHolder.Callback so we get notified when the + // underlying surface is created and destroyed. + addCallback(this@CameraPreview) + // deprecated setting, but required on Android versions prior to 3.0 + setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) + } + + override fun surfaceCreated(holder: SurfaceHolder) { + // The Surface has been created, now tell the camera where to draw the preview. + mCamera.apply { + try { + setPreviewDisplay(holder) + startPreview() + } catch (e: IOException) { + Log.d(TAG, "Error setting camera preview: ${e.message}") + } + } + } + + override fun surfaceDestroyed(holder: SurfaceHolder) { + // empty. Take care of releasing the Camera preview in your activity. + } + + override fun surfaceChanged(holder: SurfaceHolder, format: Int, w: Int, h: Int) { + // If your preview can change or rotate, take care of those events here. + // Make sure to stop the preview before resizing or reformatting it. + if (mHolder.surface == null) { + // preview surface does not exist + return + } + + // stop preview before making changes + try { + mCamera.stopPreview() + } catch (e: Exception) { + // ignore: tried to stop a non-existent preview + } + + // set preview size and make any resize, rotate or + // reformatting changes here + + // start preview with new settings + mCamera.apply { + try { + setPreviewDisplay(mHolder) + startPreview() + } catch (e: Exception) { + Log.d(TAG, "Error starting camera preview: ${e.message}") + } + } + } +} diff --git a/CameraShot/app/src/main/java/lfainsin/enseeiht/camerashot/MainActivity.kt b/CameraShot/app/src/main/java/lfainsin/enseeiht/camerashot/MainActivity.kt new file mode 100644 index 0000000..c0e7d0d --- /dev/null +++ b/CameraShot/app/src/main/java/lfainsin/enseeiht/camerashot/MainActivity.kt @@ -0,0 +1,182 @@ +package lfainsin.enseeiht.camerashot + +import CameraPreview +import android.content.ContentValues.TAG +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Matrix +import android.hardware.Camera +import android.hardware.Camera.CameraInfo +import android.os.Bundle +import android.os.Environment +import android.util.Log +import android.widget.Button +import android.widget.FrameLayout +import android.widget.ImageView +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import java.io.* +import java.text.SimpleDateFormat +import java.util.* + + +class MainActivity : AppCompatActivity() { + private var mCamera: Camera? = null + private var mPreview: CameraPreview? = null + private var mButton: Button? = null + private var mSnapshot: ImageView? = null + + /** Get the front facing camera */ + private fun openFrontFacingCamera(): Camera? { + var cam: Camera? = null + val cameraInfo = CameraInfo() + for (i in 0 until Camera.getNumberOfCameras()) { + Camera.getCameraInfo(i, cameraInfo) + if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) { + try { + cam = Camera.open(i) + cam.setDisplayOrientation(90) + } catch (e: RuntimeException) { + Log.e(TAG, "Camera failed to open: " + e.localizedMessage) + } + } + } + return cam + } + + /** Create a File for saving an image or video */ + private fun getOutputMediaFile(type: Int): File? { + // Get the directory where we'll save photos + val dir = File( + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), + R.string.app_name.toString() + ) + + // Create the storage directory if it does not exist + dir.apply { + if (!exists()) { + Log.e(R.string.app_name.toString(), "${dir.path} doesn't exists") + if (!mkdirs()) { + Log.e(R.string.app_name.toString(), "failed to create directory") + return null + } + } + } + + // Create a media file name + val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date()) + return when (type) { + MEDIA_TYPE_IMAGE -> { + File("${dir.path}${File.separator}IMG_$timeStamp.jpg") + } + MEDIA_TYPE_VIDEO -> { + File("${dir.path}${File.separator}VID_$timeStamp.mp4") + } + else -> null + } + } + + private val mPicture = Camera.PictureCallback { data, _ -> + // rotate the image like an idiot + var bmp = BitmapFactory.decodeByteArray(data, 0, data.size, null) + val mat: Matrix = Matrix().apply { setRotate(-90F) } + bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.width, bmp.height, mat, true) + val stream = ByteArrayOutputStream() + bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream) + + // Create the file in which we'll save the image + val pictureFile: File = getOutputMediaFile(MEDIA_TYPE_IMAGE) ?: run { + Log.e(TAG, ("Error creating media file, check storage permissions")) + return@PictureCallback + } + + try { + // Save the image to the file + val fos = FileOutputStream(pictureFile) + fos.write(stream.toByteArray()) + fos.close() + Log.d(TAG, "Wrote to file: $pictureFile") + + // Display the image below the preview + mSnapshot?.setImageBitmap(BitmapFactory.decodeFile(pictureFile.toString())) + + // Restart the preview after the photo has been taken + mCamera?.stopPreview() + mCamera?.startPreview() + } catch (e: FileNotFoundException) { + Log.d(TAG, "File not found: ${e.message}") + } catch (e: IOException) { + Log.d(TAG, "Error accessing file: ${e.message}") + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + // Get the ImageView + mSnapshot = findViewById(R.id.imageView) + + // Create an instance of Camera + mCamera = openFrontFacingCamera() + mCamera?.setDisplayOrientation(90) + + // Create our Preview view + mPreview = mCamera?.let { + CameraPreview(this, it) + } + + // Set the Preview view as the content of our activity. + mPreview?.also { + val preview: FrameLayout = findViewById(R.id.framelayout) + preview.addView(it) + } + + mButton = findViewById(R.id.button) + mButton?.setOnClickListener { + // get an image from the camera + mCamera?.takePicture(null, null, mPicture) + Toast.makeText(this, "Picture taken", Toast.LENGTH_SHORT).show() + } + } + + override fun onPause() { + super.onPause() + + // Release the camera for other applications + mCamera?.stopPreview() + mCamera?.release() + mCamera = null + + // Release the Preview view + mPreview = null + } + + override fun onResume() { + super.onResume() + + // Create an instance of Camera, if necessary + mCamera ?: run { + mCamera = openFrontFacingCamera() + mCamera?.setDisplayOrientation(90) + } + + // Create our Preview view, if necessary + mPreview ?: run { + mPreview = mCamera?.let { + CameraPreview(this, it) + } + + // Set the Preview view as the content of our activity. + mPreview?.also { + val preview: FrameLayout = findViewById(R.id.framelayout) + preview.addView(it) + } + } + } + + companion object { + private const val MEDIA_TYPE_IMAGE = 1 + private const val MEDIA_TYPE_VIDEO = 2 + } +} \ No newline at end of file diff --git a/CameraShot/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/CameraShot/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/CameraShot/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/CameraShot/app/src/main/res/drawable/ic_launcher_background.xml b/CameraShot/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/CameraShot/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CameraShot/app/src/main/res/layout/activity_main.xml b/CameraShot/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..b1895dc --- /dev/null +++ b/CameraShot/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,34 @@ + + + + + + + +