-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCameraxActivity.java
More file actions
182 lines (162 loc) · 6.54 KB
/
CameraxActivity.java
File metadata and controls
182 lines (162 loc) · 6.54 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.asmaamir.mlkitdemo.CameraX;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraInfoUnavailableException;
import androidx.camera.core.CameraX;
import androidx.camera.core.CameraX.LensFacing;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageAnalysisConfig;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureConfig;
import androidx.camera.core.Preview;
import androidx.camera.core.PreviewConfig;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.asmaamir.mlkitdemo.R;
import java.io.File;
/*
* Example: https://codelabs.developers.google.com/codelabs/camerax-getting-started/#0
*/
public class CameraxActivity extends AppCompatActivity {
private TextureView tv;
private static final String TAG = "CameraxActivity";
public static final int REQUEST_CODE_PERMISSION = 101;
public static final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};
public static LensFacing lens = LensFacing.FRONT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camerax);
tv = findViewById(R.id.texture_view);
if (allPermissionsGranted()) {
tv.post(this::startCamera);
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSION);
}
tv.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> updateTransform());
}
@SuppressLint("RestrictedApi")
private void startCamera() {
initCamera();
ImageButton ibSwitch = findViewById(R.id.btn_switch);
ibSwitch.setOnClickListener(v -> {
if (lens == LensFacing.FRONT)
lens = LensFacing.BACK;
else
lens = LensFacing.FRONT;
try {
Log.i(TAG, "" + lens);
CameraX.getCameraWithLensFacing(lens);
initCamera();
} catch (CameraInfoUnavailableException e) {
Log.e(TAG, e.toString());
}
});
}
private void initCamera() {
CameraX.unbindAll();
PreviewConfig pc = new PreviewConfig
.Builder()
.setLensFacing(lens)
.setTargetResolution(new Size(tv.getWidth(), tv.getHeight()))
.build();
Preview preview = new Preview(pc);
preview.setOnPreviewOutputUpdateListener(output -> {
ViewGroup vg = (ViewGroup) tv.getParent();
vg.removeView(tv);
vg.addView(tv, 0);
tv.setSurfaceTexture(output.getSurfaceTexture());
updateTransform();
});
ImageCaptureConfig icc = new ImageCaptureConfig
.Builder()
.setLensFacing(lens)
.setCaptureMode(ImageCapture.CaptureMode.MAX_QUALITY)
.build();
ImageCapture imgCap = new ImageCapture(icc);
ImageButton ib = findViewById(R.id.img_cap);
ib.setOnClickListener(v -> {
File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");
imgCap.takePicture(file, (Runnable::run), new ImageCapture.OnImageSavedListener() {
@Override
public void onImageSaved(@NonNull File file) {
String msg = "Image is saved at: " + file.getAbsolutePath();
runOnUiThread(() -> Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show());
Log.i(TAG, msg);
}
@Override
public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
Log.e(TAG, "An error occurred while saving:" + message);
}
});
});
ImageAnalysisConfig iac = new ImageAnalysisConfig
.Builder()
.setLensFacing(lens)
.setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
.build();
ImageAnalysis imageAnalysis = new ImageAnalysis(iac);
imageAnalysis.setAnalyzer(Runnable::run,
new DemoAnalyzer());
CameraX.bindToLifecycle(this, preview, imgCap, imageAnalysis);
}
private void updateTransform() {
Matrix mat = new Matrix();
float centerX = tv.getWidth() / 2.0f;
float centerY = tv.getHeight() / 2.0f;
float rotationDegrees;
switch (tv.getDisplay().getRotation()) {
case Surface.ROTATION_0:
rotationDegrees = 0;
break;
case Surface.ROTATION_90:
rotationDegrees = 90;
break;
case Surface.ROTATION_180:
rotationDegrees = 180;
break;
case Surface.ROTATION_270:
rotationDegrees = 270;
break;
default:
return;
}
mat.postRotate(rotationDegrees, centerX, centerY);
tv.setTransform(mat);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (allPermissionsGranted()) {
tv.post(this::startCamera);
} else {
Toast.makeText(this,
"Permissions not granted by the user.",
Toast.LENGTH_SHORT).show();
finish();
}
}
}
private boolean allPermissionsGranted() {
for (String permission : REQUIRED_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
}