0. 출처: https://www.edwith.org/boostcourse-android/lecture/17103/



1. 실행 결과 화면

  



2. activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context="com.tistory.qlyh8.practice.MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="사진 찍기"/>
 
        <ImageView
            android:id="@+id/image_view"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_gravity="center"/>
    </LinearLayout>
 
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2">
        <com.tistory.qlyh8.practice.CameraSurfaceView
            android:id="@+id/surface_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
 
</LinearLayout>
 
cs



3. AndroidManifest.xml

인터넷 권한 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tistory.qlyh8.practice">
 
    <uses-permission android:name="android.permission.CAMERA"/>
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
cs



4. CameraSurfaceView.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.tistory.qlyh8.practice;
 
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
 
import java.io.IOException;
 
// SurfaceView 를 사용하여 카메라 미리 보기를 추가
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
 
    // SurfaceView 는 껍데기 역할만 하고 컨트롤은 SurfaceHolder 가 담당
    SurfaceHolder holder;
    Camera camera = null;
 
    public CameraSurfaceView(Context context) {
        super(context);
        init(context);
    }
 
    public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
 
    private void init(Context context){
        holder = getHolder();
        holder.addCallback(this);
    }
 
    // SurfaceView 가 만들어지는 시점에 호출
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // 카메라 객체 오픈
        camera = Camera.open();
        try {
            // 카메라 객체에 SurfaceView 를 미리보기로 사용
            camera.setPreviewDisplay(holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    // SurfaceViw 가 변경되는 시점에 호출, 화면에 보여지기 전에 크기를 결정
    @Override
    public void surfaceChanged(SurfaceHolder holder, int formatint width, int height) {
        camera.startPreview(); // 미리보기 화면에 렌즈로부터 들어온 영상을 뿌려줌
 
    }
 
    // SurfaceView 가 소멸하는 시점에 호출
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview(); // 미리보기 중지
        camera.release();   // 리소스 해제
        camera = null;
    }
 
    // 사진 찍기
    public boolean capture(Camera.PictureCallback callback){
        if(camera != null){
            camera.takePicture(nullnull, callback);
            return true;
        } else{
            return false;
        }
    }
}
 
cs



5. MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.tistory.qlyh8.practice;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
 
 
public class MainActivity extends AppCompatActivity {
 
    private ImageView imageView;
    private CameraSurfaceView surfaceView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        imageView = findViewById(R.id.image_view);
        surfaceView = findViewById(R.id.surface_view);
 
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                surfaceView.capture(new Camera.PictureCallback() {
                    public void onPictureTaken(byte[] data, Camera camera) {
                        // 사진 찍은 결과가 byte[]로 전달된 것을 이미지뷰로 보여주기
                        // 이미지 resizing
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        // 8분의 1크기로 비트맵 객체 생성
                        options.inSampleSize = 8;
                        // 가져온 결과물을 비트맵 객체로 생성
                        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                        // 이미지뷰에 비트맵 객체 설정
                        imageView.setImageBitmap(bitmap);
                        // 사진을 찍으면 미리보기가 중지되기 때문에 다시 시작하게 함
                        camera.startPreview();
                    }
                });
            }
        });
    }
}
cs



6. build.grade (app)

위험권한이기 때문에 불필요한 코드는 추가하지 않도록 targetSdkVersion을 22로 낮춘다.



7. 참고

- Camera API: https://developer.android.com/guide/topics/media/camera

- Control the camera: https://developer.android.com/training/camera/cameradirect

- Take Photos: https://developer.android.com/training/camera/photobasics

'Android' 카테고리의 다른 글

동영상 재생하기  (0) 2018.08.23
음악 재생하기  (0) 2018.08.23
사진 찍어 보여주기  (0) 2018.08.23
인터넷 연결상태 확인하기  (0) 2018.08.19
SQLiteOpenHelper 사용해보기  (0) 2018.08.19

+ Recent posts