package com.example.unit_testing
import android.content.Intent
import android.graphics.Bitmap
import android.provider.MediaStore
import android.view.WindowManager.LayoutParams
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel
class MainActivity : FlutterActivity() {
private val METHOD_CHANNEL_NAME = "com.jinto/method"
private var methodChannel: MethodChannel? = null
private val cameraRequest = 1888
var imageView: Bitmap? = null
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
window.addFlags(LayoutParams.FLAG_SECURE)
super.configureFlutterEngine(flutterEngine)
// Setup Channels
setupChannels(flutterEngine.dartExecutor.binaryMessenger)
}
override fun onDestroy() {
teardownChannels()
super.onDestroy()
}
private fun setupChannels(messenger: BinaryMessenger) {
methodChannel = MethodChannel(messenger, METHOD_CHANNEL_NAME)
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
methodChannel!!.setMethodCallHandler { call, result ->
if (call.method == "cameraOn") {
startActivityForResult(cameraIntent, cameraRequest)
result.success(imageView)
} else {
result.notImplemented()
}
}
}
private fun teardownChannels() {
methodChannel!!.setMethodCallHandler(null)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == cameraRequest && resultCode == RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap
println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww${imageBitmap.width}")
imageView = imageBitmap
}
}
}
如果我们通过 bitmap(imageView) 它将不起作用,最初它什么都不重新调整,在相机服务开始没有获取任何数据之后
我如何将位图转换为文件并返回它,你能帮我解决这个问题吗?
这是我从平台端获取数据的代码,
Future<void> _checkAvailability() async {
try {
var apiImage = await methodChannel.invokeMethod("cameraOn");
print('$apiImage');
} on PlatformException catch (e) {
print('error');
print(e);
}
}