Skip to content

Fix 摄像头切换crash & 升级gradle版本 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
implementation 'com.android.support:recyclerview-v7:26.1.0'
compile 'de.hdodenhof:circleimageview:2.1.0'
implementation 'de.hdodenhof:circleimageview:2.1.0'
}
59 changes: 32 additions & 27 deletions app/src/main/java/com/example/ice/coursetable/CameraActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ private void changeCamera(){
mCameraDevice = null;
}
mCameraID^=1;
initImageReader();
mCameraManager.openCamera(mCameraID+"", stateCallback, mainHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
Expand All @@ -212,33 +213,7 @@ private void initCamera2() {

mCameraID = BACK_CAMERA;//后摄像头
Log.d(TAG, "initCamera2: "+ CameraCharacteristics.LENS_FACING_BACK);
mImageReader = ImageReader.newInstance(1080, 1920, ImageFormat.JPEG,1);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { //可以在这里处理拍照得到的临时照片 例如,写入本地
@Override
public void onImageAvailable(ImageReader reader) {
// mCameraDevice.close();
// mSurfaceView.setVisibility(View.INVISIBLE);
// 拿到拍照照片数据
Image image = reader.acquireNextImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);//由缓冲区存入字节数组
//保存

Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

if (bitmap != null) {
if(mCameraID == FRONT_CAMERA){
//前置摄像头拍的要先旋转180度
bitmap = adjustPhotoRotation(bitmap,180);
}
iv_thumb.setImageBitmap(bitmap);
writeToFile(bitmap);
}

image.close();
}
}, mainHandler);
initImageReader();
//获取摄像头管理
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

Expand All @@ -254,6 +229,36 @@ public void onImageAvailable(ImageReader reader) {
}
}

private void initImageReader(){
mImageReader = ImageReader.newInstance(1080, 1920, ImageFormat.JPEG,1);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { //可以在这里处理拍照得到的临时照片 例如,写入本地
@Override
public void onImageAvailable(ImageReader reader) {
// mCameraDevice.close();
// mSurfaceView.setVisibility(View.INVISIBLE);
// 拿到拍照照片数据
Image image = reader.acquireNextImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);//由缓冲区存入字节数组
//保存

Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

if (bitmap != null) {
if(mCameraID == FRONT_CAMERA){
//前置摄像头拍的要先旋转180度
bitmap = adjustPhotoRotation(bitmap,180);
}
iv_thumb.setImageBitmap(bitmap);
writeToFile(bitmap);
}

image.close();
}
}, mainHandler);
}

Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree)
{
Matrix m = new Matrix();
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.android.tools.build:gradle:3.3.1'


// NOTE: Do not place your application dependencies here; they belong
Expand Down