본문 바로가기
App Programming/Android Studio

[Android Studio] 카메라 (Camera)

by goatlab 2023. 5. 30.
728x90
반응형
SMALL

카메라 (Camera)

 

Android 프레임워크에는 기기에서 이용 가능한 다양한 카메라와 카메라 기능에 대한 지원을 포함하여 애플리케이션에서 사진과 동영상을 캡처할 수 있도록 한다.

 

xml : main

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main">
    
    <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Take a picture" />
    
    <ImageView
  	android:id="@+id/imageView"
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

java : main

 

import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class main extends AppCompatActivity {
    private static final int REQUEST_CODE_CAMERA = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
		
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
    	@Override
    	public void onClick(View v) {
        	takePicture();
    	}
		});
    }
    
    private void takePicture() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CODE_CAMERA);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        ImageView imageView = findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);

        // Activity2로 전환
        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra("bitmap", bitmap);
        startActivity(intent);
    }
}
}

 

xml : Activity2

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity2">
    
    <ImageView
  	android:id="@+id/imageView2"
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

java : Activity2

 

import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Activity2 extends AppCompatActivity {
    private static final int REQUEST_CODE_CAMERA = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        // main에서 전달받은 이미지를 ImageView에 표시
        ImageView imageView = findViewById(R.id.imageView2);
        Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
        imageView.setImageBitmap(bitmap);
    }
}

728x90
반응형
LIST