안드로이드 5.0 이상의 버전은 Camera API 대신 Camera2 API를 사용할 수 있다.
이 코드는 안드로이드 6.0에서도 사용이 가능하도록 하였다.
-------------------------------------------------------------------------------------------------------------------
public class MainActivity extends AppCompatActivity {
private TextureView mCameraTextureView;
private Preview mPreview;
Activity mainActivity = this;
private static final String TAG = "MAINACTIVITY";
static final int REQUEST_CAMERA = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCameraTextureView = (TextureView) findViewById(R.id.cameraTextureView);
mPreview = new Preview(this, mCameraTextureView);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CAMERA:
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (permission.equals(Manifest.permission.CAMERA)) {
if(grantResult == PackageManager.PERMISSION_GRANTED) {
mCameraTextureView = (TextureView) findViewById(R.id.cameraTextureView);
mPreview = new Preview(mainActivity, mCameraTextureView);
Log.d(TAG,"mPreview set");
} else {
Toast.makeText(this,"Should have camera permission to run", Toast.LENGTH_LONG).show();
finish();
}
}
}
break;
}
}
@Override
protected void onResume() {
super.onResume();
mPreview.onResume();
}
@Override
protected void onPause() {
super.onPause();
mPreview.onPause();
}
}
-----------------------------------------------------------------------------------------------------------------
위는 이코드의 MainActivity이다. 카메라의 화면을 보여주는 TextureView와 카메라의 기능을 하는 Preview를 선언하였다.
-----------------------------------------------------------------------------------------------------------------
public class Preview extends Thread {
private final static String TAG = "Preview : ";
private Size mPreviewSize;
private Context mContext;
private CameraDevice mCameraDevice;
private CaptureRequest.Builder mPreviewBuilder;
private CameraCaptureSession mPreviewSession;
private TextureView mTextureView;
public Preview(Context context, TextureView textureView) {
mContext = context;
mTextureView = textureView;
}
private String getBackFacingCameraId(CameraManager cManager) {
try {
for (final String cameraId : cManager.getCameraIdList()) {
CameraCharacteristics characteristics = cManager.getCameraCharacteristics(cameraId);
int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
if (cOrientation == CameraCharacteristics.LENS_FACING_BACK) return cameraId;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
return null;
}
public void openCamera() {
CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
Log.e(TAG, "openCamera E");
try {
String cameraId = getBackFacingCameraId(manager);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
int permissionCamera = ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA);
if(permissionCamera == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.CAMERA}, MainActivity.REQUEST_CAMERA);
} else {
manager.openCamera(cameraId, mStateCallback, null);
}
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(TAG, "openCamera X");
}
private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener(){
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface,
int width, int height) {
// TODO Auto-generated method stub
Log.e(TAG, "onSurfaceTextureAvailable, width="+width+",height="+height);
openCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface,
int width, int height) {
// TODO Auto-generated method stub
Log.e(TAG, "onSurfaceTextureSizeChanged");
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// TODO Auto-generated method stub
}
};
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
// TODO Auto-generated method stub
Log.e(TAG, "onOpened");
mCameraDevice = camera;
startPreview();
}
@Override
public void onDisconnected(CameraDevice camera) {
// TODO Auto-generated method stub
Log.e(TAG, "onDisconnected");
}
@Override
public void onError(CameraDevice camera, int error) {
// TODO Auto-generated method stub
Log.e(TAG, "onError");
}
};
protected void startPreview() {
// TODO Auto-generated method stub
if(null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
Log.e(TAG, "startPreview fail, return");
}
SurfaceTexture texture = mTextureView.getSurfaceTexture();
if(null == texture) {
Log.e(TAG,"texture is null, return");
return;
}
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface surface = new Surface(texture);
try {
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPreviewBuilder.addTarget(surface);
try {
mCameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
// TODO Auto-generated method stub
mPreviewSession = session;
updatePreview();
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "onConfigureFailed", Toast.LENGTH_LONG).show();
}
}, null);
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void updatePreview() {
// TODO Auto-generated method stub
if(null == mCameraDevice) {
Log.e(TAG, "updatePreview error, return");
}
mPreviewBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
HandlerThread thread = new HandlerThread("CameraPreview");
thread.start();
Handler backgroundHandler = new Handler(thread.getLooper());
try {
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, backgroundHandler);
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setSurfaceTextureListener()
{
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
public void onResume() {
Log.d(TAG, "onResume");
setSurfaceTextureListener();
}
private Semaphore mCameraOpenCloseLock = new Semaphore(1);
public void onPause() {
// TODO Auto-generated method stub
Log.d(TAG, "onPause");
try {
mCameraOpenCloseLock.acquire();
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice = null;
Log.d(TAG, "CameraDevice Close");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.");
} finally {
mCameraOpenCloseLock.release();
}
}
}
----------------------------------------------------------------------------------------------------------------------
Camera2 API는 기존의 Camera API와 다르게 선언해야하는 여러가지 Camera 클래스들이 있다.
CameraDevice는 카메라 기기를 나타내고 CameraManager는 카메라의 기능을 시키는 요소이다.
카메라를 켜기위해 openCamera를 사용하였는데
이 함수는 SurfaceTextureListener내부의 onSurfaceTextureAvailable에 위치한다
TextureView가 사용이 가능하다면 카메라를 켜는 기능이다.
----------------------------------------------------------------------------------------------------------------------
public void openCamera() {
CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
Log.e(TAG, "openCamera E");
try {
String cameraId = getBackFacingCameraId(manager);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
int permissionCamera = ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA);
if(permissionCamera == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.CAMERA}, MainActivity.REQUEST_CAMERA);
} else {
manager.openCamera(cameraId, mStateCallback, null);
}
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(TAG, "openCamera X");
}
----------------------------------------------------------------------------------------------------------------------
또한 카메라를 다시 켜고 싶다면, SurfaceTextureListener만 다시 선언해 주면 된다.
----------------------------------------------------------------------------------------------------------------------
public void setSurfaceTextureListener()
{
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
----------------------------------------------------------------------------------------------------------------------
카메라가 켜진다면 카메라 상태 콜백함수가 실행된다.
----------------------------------------------------------------------------------------------------------------------
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
// TODO Auto-generated method stub
Log.e(TAG, "onOpened");
mCameraDevice = camera;
startPreview();
}
@Override
public void onDisconnected(CameraDevice camera) {
// TODO Auto-generated method stub
Log.e(TAG, "onDisconnected");
}
@Override
public void onError(CameraDevice camera, int error) {
// TODO Auto-generated method stub
Log.e(TAG, "onError");
}
};
----------------------------------------------------------------------------------------------------------------------
이 함수 내부에 onOpened함수가 있는데 카메라가 켜지면 자동적으로 실행된다.
여기서 실질적으로 하는 함수는 startPreview함수이다.
----------------------------------------------------------------------------------------------------------------------
protected void startPreview() {
// TODO Auto-generated method stub
if(null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
Log.e(TAG, "startPreview fail, return");
}
SurfaceTexture texture = mTextureView.getSurfaceTexture();
if(null == texture) {
Log.e(TAG,"texture is null, return");
return;
}
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface surface = new Surface(texture);
try {
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPreviewBuilder.addTarget(surface);
try {
mCameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
// TODO Auto-generated method stub
mPreviewSession = session;
updatePreview();
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "onConfigureFailed", Toast.LENGTH_LONG).show();
}
}, null);
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
-----------------------------------------------------------------------------------------------------------------------
startPreview함수는 위와 같다. 여기서는 카메라의 출력을 어디로 보낼 것인지 설정한다.
출력은 최대 3개까지 지정할 수 있다.
여기서는 카메라 프리뷰만 설정하였는데, 사진촬영 동영상촬영은 이 함수 내에 코드를 몇개만 바꿔서 사용할 수 있다.
카메라의 출력은 surface로 저장되어 mPreviewBuilder에 들어가게 된다.
-----------------------------------------------------------------------------------------------------------------------
protected void updatePreview() {
// TODO Auto-generated method stub
if(null == mCameraDevice) {
Log.e(TAG, "updatePreview error, return");
}
mPreviewBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
HandlerThread thread = new HandlerThread("CameraPreview");
thread.start();
Handler backgroundHandler = new Handler(thread.getLooper());
try {
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, backgroundHandler);
} catch (CameraAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
-----------------------------------------------------------------------------------------------------------------------
마지막으로 updatePreview함수가 실행된다.
이 함수는 화면의 프리뷰를 계속 변경시켜주는 Thread를 실행시키는 함수이다.
이것으로 카메라의 프리뷰가 실행된다.
아래는 Camera2 API를 이해하기 위해 코드이다.
Camera2Raw : https://github.com/googlesamples/android-Camera2Raw
예제로 사용한 코드는 GitHub에 업로드 하였다.
mapp : https://github.com/pchan1401-ICIL/mapp