Created
February 14, 2021 11:09
-
-
Save doyle-flutter/d048cbeb92ca20d8d796b4fdbe53bff9 to your computer and use it in GitHub Desktop.
Android & Flutter 05 Camera++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.doyle.andtest; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.os.Bundle; | |
import android.provider.MediaStore; | |
import androidx.annotation.NonNull; | |
import java.io.ByteArrayOutputStream; | |
import io.flutter.embedding.android.FlutterActivity; | |
import io.flutter.embedding.engine.FlutterEngine; | |
import io.flutter.plugin.common.MethodChannel; | |
public class MainActivity extends FlutterActivity { | |
private static final String CHANNEL = "app.james.cam/cam"; | |
private static final String SEND_CHANNEL = "app.james.cam/cam2"; | |
MethodChannel sendChannel; | |
@Override | |
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { | |
super.configureFlutterEngine(flutterEngine); | |
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL) | |
.setMethodCallHandler( | |
((call, result) -> { | |
if(call.method.equals("cam")){ | |
dispatchTakePictureIntent(); | |
} | |
})); | |
sendChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), SEND_CHANNEL); | |
} | |
// 04 영상 | |
// static final int REQUEST_IMAGE_CAPTURE = 1; | |
// @SuppressLint("QueryPermissionsNeeded") | |
// private void dispatchTakePictureIntent() { | |
// Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
// if (takePictureIntent.resolveActivity(getPackageManager()) != null) { | |
// startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); | |
// } | |
// } | |
// 05 영상 | |
static final int REQUEST_IMAGE_CAPTURE = 1; | |
private void dispatchTakePictureIntent() { | |
// [ UI 및 제어는 플러터에서 만들기 때문에 주석 | |
// if(this.imageBitmap != null){ | |
// this.clear(); | |
// button.setText("촬영"); ] | |
// return; | |
// } | |
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
if (takePictureIntent.resolveActivity(getPackageManager()) != null) { | |
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { | |
Bundle extras = data.getExtras(); | |
Bitmap imageBitmap = (Bitmap) extras.get("data"); | |
ByteArrayOutputStream stream = new ByteArrayOutputStream() ; | |
imageBitmap.compress( Bitmap.CompressFormat.PNG, 100, stream) ; | |
byte[] byteArray = stream.toByteArray() ; | |
// [ UI 및 제어는 플러터에서 만들기 때문에 주석 | |
// iv.setImageBitmap(imageBitmap); | |
// button.setText("지우기"); ] | |
sendChannel.invokeMethod("sendImg", byteArray); | |
} | |
} | |
// void clear(){ | |
// [ UI 및 제어는 플러터에서 만들기 때문에 주석 | |
// iv.setImageBitmap(null); ] | |
// this.imageBitmap = null; | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment