Skip to content

Commit ce55f20

Browse files
committed
Changes.
1 parent d5a73a4 commit ce55f20

55 files changed

Lines changed: 2194 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

android/src/main/kotlin/com/example/vouched_plugin/CameraConnectionFragment.java

Lines changed: 616 additions & 0 deletions
Large diffs are not rendered by default.

android/src/main/kotlin/com/example/vouched_plugin/DetectorActivity.java

Lines changed: 695 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package com.example.vouched_plugin;
2+
3+
/*
4+
* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import android.annotation.SuppressLint;
20+
import android.app.Fragment;
21+
import android.graphics.SurfaceTexture;
22+
import android.hardware.Camera;
23+
import android.hardware.Camera.CameraInfo;
24+
import android.os.Bundle;
25+
import android.os.Handler;
26+
import android.os.HandlerThread;
27+
import android.util.Size;
28+
import android.util.SparseIntArray;
29+
import android.view.LayoutInflater;
30+
import android.view.Surface;
31+
import android.view.TextureView;
32+
import android.view.View;
33+
import android.view.ViewGroup;
34+
35+
import java.io.IOException;
36+
import java.util.List;
37+
38+
import id.vouched.android.customview.AutoFitTextureView;
39+
import id.vouched.android.env.ImageUtils;
40+
41+
@SuppressLint("ValidFragment")
42+
public class LegacyCameraConnectionFragment extends Fragment {
43+
/**
44+
* Conversion from screen rotation to JPEG orientation.
45+
*/
46+
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
47+
48+
static {
49+
ORIENTATIONS.append(Surface.ROTATION_0, 90);
50+
ORIENTATIONS.append(Surface.ROTATION_90, 0);
51+
ORIENTATIONS.append(Surface.ROTATION_180, 270);
52+
ORIENTATIONS.append(Surface.ROTATION_270, 180);
53+
}
54+
55+
private Camera camera;
56+
private Camera.PreviewCallback imageListener;
57+
private Size desiredSize;
58+
/**
59+
* The layout identifier to inflate for this Fragment.
60+
*/
61+
private int layout;
62+
/**
63+
* An {@link AutoFitTextureView} for camera preview.
64+
*/
65+
private AutoFitTextureView textureView;
66+
private SurfaceTexture availableSurfaceTexture = null;
67+
68+
/**
69+
* {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a {@link
70+
* TextureView}.
71+
*/
72+
private final TextureView.SurfaceTextureListener surfaceTextureListener =
73+
new TextureView.SurfaceTextureListener() {
74+
@Override
75+
public void onSurfaceTextureAvailable(
76+
final SurfaceTexture texture, final int width, final int height) {
77+
availableSurfaceTexture = texture;
78+
startCamera();
79+
}
80+
81+
@Override
82+
public void onSurfaceTextureSizeChanged(
83+
final SurfaceTexture texture, final int width, final int height) {
84+
}
85+
86+
@Override
87+
public boolean onSurfaceTextureDestroyed(final SurfaceTexture texture) {
88+
return true;
89+
}
90+
91+
@Override
92+
public void onSurfaceTextureUpdated(final SurfaceTexture texture) {
93+
}
94+
};
95+
/**
96+
* An additional thread for running tasks that shouldn't block the UI.
97+
*/
98+
private HandlerThread backgroundThread;
99+
100+
public LegacyCameraConnectionFragment(
101+
final Camera.PreviewCallback imageListener, final int layout, final Size desiredSize) {
102+
this.imageListener = imageListener;
103+
this.layout = layout;
104+
this.desiredSize = desiredSize;
105+
}
106+
107+
@Override
108+
public View onCreateView(
109+
final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
110+
return inflater.inflate(layout, container, false);
111+
}
112+
113+
@Override
114+
public void onViewCreated(final View view, final Bundle savedInstanceState) {
115+
textureView = (AutoFitTextureView) view.findViewById(R.id.texture);
116+
}
117+
118+
@Override
119+
public void onActivityCreated(final Bundle savedInstanceState) {
120+
super.onActivityCreated(savedInstanceState);
121+
}
122+
123+
@Override
124+
public void onResume() {
125+
super.onResume();
126+
startBackgroundThread();
127+
// When the screen is turned off and turned back on, the SurfaceTexture is already
128+
// available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
129+
// a camera and start preview from here (otherwise, we wait until the surface is ready in
130+
// the SurfaceTextureListener).
131+
132+
if (textureView.isAvailable()) {
133+
startCamera();
134+
} else {
135+
textureView.setSurfaceTextureListener(surfaceTextureListener);
136+
}
137+
}
138+
139+
@Override
140+
public void onPause() {
141+
stopCamera();
142+
stopBackgroundThread();
143+
super.onPause();
144+
}
145+
146+
/**
147+
* Starts a background thread and its {@link Handler}.
148+
*/
149+
private void startBackgroundThread() {
150+
backgroundThread = new HandlerThread("CameraBackground");
151+
backgroundThread.start();
152+
}
153+
154+
/**
155+
* Stops the background thread and its {@link Handler}.
156+
*/
157+
private void stopBackgroundThread() {
158+
backgroundThread.quitSafely();
159+
try {
160+
backgroundThread.join();
161+
backgroundThread = null;
162+
} catch (final InterruptedException e) {
163+
e.printStackTrace();
164+
}
165+
}
166+
167+
private void startCamera() {
168+
int index = getCameraId();
169+
camera = Camera.open(index);
170+
171+
try {
172+
Camera.Parameters parameters = camera.getParameters();
173+
List<String> focusModes = parameters.getSupportedFocusModes();
174+
if (focusModes != null
175+
&& focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
176+
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
177+
}
178+
List<Camera.Size> cameraSizes = parameters.getSupportedPreviewSizes();
179+
Size[] sizes = new Size[cameraSizes.size()];
180+
int i = 0;
181+
for (Camera.Size size : cameraSizes) {
182+
sizes[i++] = new Size(size.width, size.height);
183+
}
184+
Size previewSize =
185+
CameraConnectionFragment.chooseOptimalSize(
186+
sizes, desiredSize.getWidth(), desiredSize.getHeight());
187+
parameters.setPreviewSize(previewSize.getWidth(), previewSize.getHeight());
188+
camera.setDisplayOrientation(90);
189+
camera.setParameters(parameters);
190+
camera.setPreviewTexture(availableSurfaceTexture);
191+
} catch (IOException exception) {
192+
camera.release();
193+
}
194+
195+
camera.setPreviewCallbackWithBuffer(imageListener);
196+
Camera.Size s = camera.getParameters().getPreviewSize();
197+
camera.addCallbackBuffer(new byte[ImageUtils.getYUVByteSize(s.height, s.width)]);
198+
199+
textureView.setAspectRatio(s.height, s.width);
200+
201+
camera.startPreview();
202+
}
203+
204+
protected void stopCamera() {
205+
if (camera != null) {
206+
camera.stopPreview();
207+
camera.setPreviewCallback(null);
208+
camera.release();
209+
camera = null;
210+
}
211+
}
212+
213+
private int getCameraId() {
214+
CameraInfo ci = new CameraInfo();
215+
for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
216+
Camera.getCameraInfo(i, ci);
217+
if (ci.facing == CameraInfo.CAMERA_FACING_BACK) return i;
218+
}
219+
return -1; // No camera found
220+
}
221+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.example.vouched_plugin
2+
3+
class NativeView {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.example.vouched_plugin
2+
3+
class NativeViewFactory {
4+
}
1.38 KB
Loading
929 Bytes
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0"/>
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0"/>
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1"/>
34+
</vector>
2.95 KB
Loading
597 Bytes
Loading

0 commit comments

Comments
 (0)