Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies {
implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.13.0'
implementation 'androidx.core:core-ktx:1.3.0-alpha01'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6'
implementation 'com.google.code.gson:gson:2.8.6'
}

afterEvaluate {
Expand Down
5 changes: 3 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
package="com.difrancescogianmarco.arcore_flutter_plugin">

<uses-permission android:name="android.permission.CAMERA" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Indicates that app requires ARCore ("AR Required"). Ensures app is only
visible in the Google Play Store on devices that support ARCore.
-->
Expand All @@ -11,7 +12,7 @@
<!-- Sceneform requires OpenGL ES 3.0 or later. -->
<uses-feature android:glEsVersion="0x00030000" android:required="true" />

<application>
<application android:requestLegacyExternalStorage="true">
<!-- Indicates that app requires ARCore ("AR Required"). Causes Google
Play Store to download and install ARCore when the app is installed.
-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.util.Pair
import com.difrancescogianmarco.arcore_flutter_plugin.flutter_models.FlutterArCoreNode
import com.difrancescogianmarco.arcore_flutter_plugin.flutter_models.FlutterArCorePose
import com.difrancescogianmarco.arcore_flutter_plugin.utils.ArCoreUtils
import com.difrancescogianmarco.arcore_flutter_plugin.utils.ScreenshotsUtils
import com.google.ar.core.AugmentedImage
import com.google.ar.core.AugmentedImageDatabase
import com.google.ar.core.Config
Expand Down Expand Up @@ -139,6 +140,10 @@ class ArCoreAugmentedImagesView(activity: Activity, context: Context, messenger:
debugLog( "INIT AUGMENTED IMAGES")
arScenViewInit(call, result)
}
"takeScreenshot" -> {
debugLog(" Take screenshot...")
ScreenshotsUtils.onGetSnapshot(arSceneView,result,activity)
}
"load_single_image_on_db" -> {
debugLog( "load_single_image_on_db")
val map = call.arguments as HashMap<String, Any>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import com.google.ar.sceneform.*
import com.google.ar.sceneform.rendering.ModelRenderable
import com.google.ar.sceneform.rendering.Texture
import com.google.ar.sceneform.ux.AugmentedFaceNode
import com.difrancescogianmarco.arcore_flutter_plugin.utils.ScreenshotsUtils
import com.google.gson.Gson
import com.google.ar.core.Session
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
Expand Down Expand Up @@ -206,9 +209,15 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
}
"takeScreenshot" -> {
debugLog(" takeScreenshot")
takeScreenshot(call, result)
takeScreenshot(call, result)
debugLog("call::>>$call,result::>>$result")

}
// "takeScreenshot" -> {
// debugLog(" Take screenshot...")
// ScreenshotsUtils.onGetSnapshot(arSceneView,result,activity)
// debugLog("arSceneView::${arSceneView},result::$result,activity:::$activity")
// }
"loadMesh" -> {
val map = call.arguments as HashMap<String, Any>
val textureBytes = map["textureBytes"] as ByteArray
Expand All @@ -222,6 +231,13 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
debugLog("Resuming ARCore now")
onResume()
}

// "hitTest" -> {
// val map = call.arguments as HashMap<String, Any>
// val x = map["x"] as Int
// val y = map["y"] as Int
// hitTest(x,y,result)
// }
"getTrackingState" -> {
debugLog("1/3: Requested tracking state, returning that back to Flutter now")

Expand Down Expand Up @@ -326,7 +342,6 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
// Create a bitmap the size of the scene view.
val bitmap: Bitmap = Bitmap.createBitmap(arSceneView!!.getWidth(), arSceneView!!.getHeight(),
Bitmap.Config.ARGB_8888)

// Create a handler thread to offload the processing of the image.
val handlerThread = HandlerThread("PixelCopier")
handlerThread.start()
Expand All @@ -335,19 +350,27 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
PixelCopy.request(arSceneView!!, bitmap, { copyResult ->
if (copyResult === PixelCopy.SUCCESS) {
try {
saveBitmapToDisk(bitmap)
val pathSaved = saveBitmapToDisk(bitmap)
result.success(pathSaved)
debugLog("pathsaved $pathSaved")
} catch (e: IOException) {
result.success(null)
debugLog("error ${e.toString()}")
e.printStackTrace();
}
} else {
result.success(null)
debugLog("error PixelCopy failed")
}
handlerThread.quitSafely()
}, Handler(handlerThread.getLooper()))

} catch (e: Throwable) {
// Several error may come out with file handling or DOM
debugLog("takeScreenshot tryCatch ${e.toString()}")
e.printStackTrace()
result.success(null)
}
result.success(null)
//result.success(null)
}

@Throws(IOException::class)
Expand Down Expand Up @@ -406,6 +429,50 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
result.success(null)
}

// private fun screenShot() {
// val view = arSceneView!!
// // Next, create a Bitmap to hold the snapshot
// val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
//
//// Finally, call the ArSceneView's `getSnapshot()` method to capture the current frame
// view.getSnapshot { snapshot ->
// // When the snapshot is ready, save it to the Bitmap
// snapshot?.let {
// val buffer = snapshot.buffer
// buffer.rewind()
// bitmap.copyPixelsFromBuffer(buffer)
// debugLog(" bitmap ${ bitmap.copyPixelsFromBuffer(buffer)}")
// }
//
// }
// }

// fun hitTest(x: Int,y:Int,result: MethodChannel.Result) {
// val session = session ?: return
// val frame =
// try {
// session.update()
// } catch (e: CameraNotAvailableException) {
// Log.e(TAG, "Camera not available during onDrawFrame", e)
// showError("Camera not available. Try restarting the app.")
// return
// }
// val APPROXIMATE_DISTANCE_METERS = 2.0f
// if (x != null || x != null) {
// Log.i(TAG, "X and Y results 1:\n$x \n$y")
//
// // First hit test
// val hitTest1 = frame.hitTestInstantPlacement(x, y, APPROXIMATE_DISTANCE_METERS)
// Log.i(TAG, "Hit test results 1:\n$hitTest1")
// val gson = Gson()
// val json = gson.toJson(hitTest1)
// return result.success(json)
//
// } else {
// debugLog("No hit Test")
// }
// }

fun addNodeWithAnchor(flutterArCoreNode: FlutterArCoreNode, result: MethodChannel.Result) {

if (arSceneView == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.difrancescogianmarco.arcore_flutter_plugin.utils

import java.nio.ByteBuffer
import java.io.File
import java.io.OutputStream
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.Date
import android.content.pm.PackageManager;
import android.view.PixelCopy
import android.graphics.Canvas
import android.os.Handler
import android.os.Environment
import android.os.Build
import android.Manifest;
import android.graphics.Bitmap
import android.app.Activity
import android.util.Log
import io.flutter.plugin.common.MethodChannel
import com.google.ar.sceneform.ArSceneView

class ScreenshotsUtils {

companion object {

fun getPictureName(): String {

var sDate: String = SimpleDateFormat("yyyyMMddHHmmss").format(Date());

return "MyApp-" + sDate + ".png";
}


fun saveBitmap(bitmap: Bitmap,activity: Activity): String {


val externalDir = Environment.getExternalStorageDirectory().getAbsolutePath();

val sDir = externalDir + File.separator + "MyApp";

val dir = File(sDir);

val dirPath: String;

if( dir.exists() || dir.mkdir()) {
dirPath = sDir + File.separator + getPictureName();
} else {
dirPath = externalDir + File.separator + getPictureName();
}



try{

val file = File(dirPath)

// Get the file output stream
val stream: OutputStream = FileOutputStream(file)

// Compress bitmap
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)

// Flush the stream
stream.flush()

// Close stream
stream.close()


}catch (e: Exception){
e.printStackTrace()
}


return dirPath;



}

fun permissionToWrite(activity: Activity): Boolean {

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.i("Sreenshot", "Permission to write false due to version codes.");

return false;
}

var perm = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);

if(perm == PackageManager.PERMISSION_GRANTED) {
Log.i("Sreenshot", "Permission to write granted!");

return true;
}

Log.i("Sreenshot","Requesting permissions...");
activity.requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
11
);
Log.i("Sreenshot", "No permissions :(");

return false;
}


fun onGetSnapshot(arSceneView: ArSceneView?, result: MethodChannel.Result,activity: Activity){

if( !permissionToWrite(activity) ) {
Log.i("Sreenshot", "Permission to write files missing!");

result.success(null);

return;
}

if(arSceneView == null){
Log.i("Sreenshot", "Ar Scene View is NULL!");

result.success(null);

return;
}
try {

val view = arSceneView!!
// val view: ArSceneView = arFragment.getArSceneView()

val bitmapImage: Bitmap = Bitmap.createBitmap(
view.getWidth(),
view.getHeight(),
Bitmap.Config.ARGB_8888
);
Log.i("Sreenshot", "PixelCopy requesting now...");
PixelCopy.request(view, bitmapImage, { copyResult ->
if (copyResult == PixelCopy.SUCCESS) {
Log.i("Sreenshot", "PixelCopy request SUCESS. ${copyResult}");

var pathSaved: String = saveBitmap(bitmapImage,activity);

Log.i("Sreenshot", "Saved on path: ${pathSaved}");
result.success(pathSaved);

}else{
Log.i("Sreenshot", "PixelCopy request failed. ${copyResult}");
result.success(null);
}

},
Handler());

} catch (e: Exception){

e.printStackTrace()
}


}
}
}
9 changes: 9 additions & 0 deletions example/lib/screens/assets_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class _AssetsObjectState extends State<AssetsObject> {
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
String? path = await arCoreController?.snapshot();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Photo saved on $path"),));

},
child: const Icon(Icons.photo),
backgroundColor: Colors.green,
),
),
);
}
Expand Down
9 changes: 9 additions & 0 deletions example/lib/screens/augmented_images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class _AugmentedPageState extends State<AugmentedPage> {
onArCoreViewCreated: _onArCoreViewCreated,
type: ArCoreViewType.AUGMENTEDIMAGES,
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
String? path = await arCoreController?.snapshot();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Photo saved on $path"),));

},
child: const Icon(Icons.photo),
backgroundColor: Colors.green,
),
),
);
}
Expand Down
9 changes: 9 additions & 0 deletions example/lib/screens/multiple_augmented_images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class _MultipleAugmentedImagesPageState
onArCoreViewCreated: _onArCoreViewCreated,
type: ArCoreViewType.AUGMENTEDIMAGES,
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
String? path = await arCoreController?.snapshot();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Photo saved on $path"),));

},
child: const Icon(Icons.photo),
backgroundColor: Colors.green,
),
),
);
}
Expand Down
Loading