Skip to content

Instantly share code, notes, and snippets.

@doyle-flutter
Created February 14, 2021 10:55
Show Gist options
  • Save doyle-flutter/cfd85cdbf03e565f3d99918f1cd4504e to your computer and use it in GitHub Desktop.
Save doyle-flutter/cfd85cdbf03e565f3d99918f1cd4504e to your computer and use it in GitHub Desktop.
Android & Flutter 05 Camera++
public class MainActivity3 extends AppCompatActivity {
ImageView iv;
Bitmap imageBitmap;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
ConstraintLayout c = (ConstraintLayout) findViewById(R.id.main3);
button = new Button(this.getApplicationContext());
button.setText("촬영");
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
dispatchTakePictureIntent();
}
};
button.setOnClickListener(listener);
iv = new ImageView(this.getApplicationContext());
iv.setBackgroundColor(Color.parseColor("#CCCCCC"));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
int ivSize = Math.round(getResources().getDisplayMetrics().density * 200);
iv.setMinimumWidth(ivSize);
iv.setMinimumHeight(ivSize);
c.addView(button);
c.addView(iv);
}
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
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();
imageBitmap = (Bitmap) extras.get("data");
iv.setImageBitmap(imageBitmap);
button.setText("지우기");
}
}
void clear(){
iv.setImageBitmap(null);
imageBitmap = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment