Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi Guys. Now we can take pictures. #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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 +13,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 @@ -16,6 +16,7 @@ import com.difrancescogianmarco.arcore_flutter_plugin.flutter_models.FlutterArCo
import com.difrancescogianmarco.arcore_flutter_plugin.flutter_models.FlutterArCorePose
import com.difrancescogianmarco.arcore_flutter_plugin.models.RotatingNode
import com.difrancescogianmarco.arcore_flutter_plugin.utils.ArCoreUtils
import com.difrancescogianmarco.arcore_flutter_plugin.utils.ScreenshotsUtils
import com.google.ar.core.*
import com.google.ar.core.exceptions.CameraNotAvailableException
import com.google.ar.core.exceptions.UnavailableException
Expand Down Expand Up @@ -162,6 +163,10 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
"init" -> {
arScenViewInit(call, result, activity)
}
"takeScreenshot" -> {
debugLog(" Take screenshot...")
ScreenshotsUtils.onGetSnapshot(arSceneView,result,activity)
}
"addArCoreNode" -> {
debugLog(" addArCoreNode")
val map = call.arguments as HashMap<String, Any>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
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 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
8 changes: 8 additions & 0 deletions example/lib/screens/hello_world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:arcore_flutter_plugin/arcore_flutter_plugin.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:vector_math/vector_math_64.dart' as vector;
import 'dart:io';

class HelloWorld extends StatefulWidget {
@override
Expand All @@ -21,6 +22,13 @@ class _HelloWorldState extends State<HelloWorld> {
body: ArCoreView(
onArCoreViewCreated: _onArCoreViewCreated,
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
String path = await arCoreController.snapshot();
},
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
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.10"
version: "0.0.11"
async:
dependency: transitive
description:
Expand Down
8 changes: 8 additions & 0 deletions lib/src/arcore_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,12 @@ class ArCoreController {
print(ex);
}
}

Future<String> snapshot() async {

final String path = await _channel.invokeMethod('takeScreenshot');

return path;
}

}