profile image

L o a d i n g . . .

반응형



안드로이드 스튜디오, 카메라로 사진 찍기

Android Studio, Take a picture with a camera


인텐트의 Action속성을 이용하여 간단하게 안드로이드에 설치되어 있는 카메라를 이용하여 사진을 찍어볼려고 한다.

카메라를 실행하면 안드로이드에 설치되어 있는 카메라를 이용학히 때문에 설정을 따로 하지 않아서 간편하다.


Using the action property of the intent, i want to take a picture simply by using the camera installed on the Android

When a camera is run, it is convenient because it does not set up the camera because it is installed in Acdroid



<RelativeLayout
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=".MainActivity">


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="28dp"
android:layout_marginTop="15dp"
android:text="Picture" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/protest1"/>
</RelativeLayout>

레이아웃은 간단하게 Button과 찍은 사진이 보일 ImageView가 있다.


layout simply have Button and ImageView that show the picture


public class MainActivity extends AppCompatActivity {

private ImageView imageView;
private Button button;

private File file;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// SD카드쪽으로 저장하도록 설정
// set to save to SDcard
File sdcard = Environment.getExternalStorageDirectory();
file = new File(sdcard, "capture.pjg");

imageView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
capture();
}
});
}

public void capture() {

// 미리 인텐트 객체에 정의된 액션정보
// 사진 앱을 띄우게 됨 ~ ACTION_IMAGE_CAPTURE
// ACtion information defined in advance on the intent object
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 어떤 파일로 저장을 할지 지정
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 101);
}

// 인텐트에 그 정보가 포함돼서 넘어오게 된다
// the intent will include that information and will be passed on
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
// 사진의 해상도가 너무 높아서
// 비트맵으로 로딩
// Loading picture with bitmap because they are too high resolution
BitmapFactory.Options options = new BitmapFactory.Options();
// 8분의 1크기로 비트맵 객체를 만든다
// create a bitmap object in an eight of the size
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);

imageView.setImageBitmap(bitmap);
}
}
}

capture 메소드는 인텐트의 ACTION_IMAGE_CAPTURE속성을 이용하여 카메라를 띄우도록 한다.

그리고 찍은 사진을 intent에 담아서 startActivityForResult를 이용하여 보낸다.

보낸 intent는 오버라이드한 onActivityResult메소드에서 받도록 한다.

여기서 기존의 찍은 사진의 용량이 크므로 비트맵을 이용하여 사이즈를 줄이도록 한다.

그 이유는 사진의 용량이 너무 클 경우 해다 앱이 종료될 수 있기 때문이다.


Capture method is run camera used ACTION_IMAGE_CAPTURE of intent

And taken a photo send used startActivityForResult that on the intent

Obtain the sent contents in the override onActivityForResult method

Since the capacity of the existing photographs is large, use bitmaps to reduce the size

This is because if the photo is too large the app could end








realize the reality



반응형
복사했습니다!