|
| 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 | +} |
0 commit comments