|
14 | 14 | import android.content.Intent; |
15 | 15 | import android.content.ServiceConnection; |
16 | 16 | import android.graphics.Bitmap; |
17 | | -import android.hardware.Camera; |
18 | 17 | import android.os.Environment; |
19 | 18 | import android.os.Handler; |
20 | 19 | import android.os.IBinder; |
|
29 | 28 | import org.havenapp.main.PreferenceManager; |
30 | 29 | import org.havenapp.main.model.EventTrigger; |
31 | 30 | import org.havenapp.main.service.MonitorService; |
32 | | -import org.jcodec.api.SequenceEncoder; |
33 | 31 | import org.jcodec.api.android.AndroidSequenceEncoder; |
34 | | -import org.jcodec.codecs.vpx.IVFMuxer; |
35 | | -import org.jcodec.codecs.vpx.VP8Encoder; |
36 | | -import org.jcodec.common.io.SeekableByteChannel; |
37 | 32 |
|
38 | 33 | import java.io.File; |
39 | 34 | import java.io.FileOutputStream; |
40 | 35 | import java.io.IOException; |
41 | | -import java.nio.ByteBuffer; |
42 | 36 | import java.util.ArrayList; |
43 | 37 | import java.util.Date; |
44 | 38 | import java.util.List; |
|
49 | 43 |
|
50 | 44 | import io.github.silvaren.easyrs.tools.Nv21Image; |
51 | 45 |
|
52 | | -public class Preview { |
| 46 | +public class CameraViewHolder { |
53 | 47 |
|
54 | 48 | /** |
55 | 49 | * Object to retrieve and set shared preferences |
@@ -118,7 +112,7 @@ public void onServiceDisconnected(ComponentName arg0) { |
118 | 112 | } |
119 | 113 | }; |
120 | 114 |
|
121 | | - public Preview (Context context, CameraView cameraView) { |
| 115 | + public CameraViewHolder(Context context, CameraView cameraView) { |
122 | 116 | //super(context); |
123 | 117 | this.context = context; |
124 | 118 | this.cameraView = cameraView; |
@@ -175,14 +169,14 @@ public void initCamera() { |
175 | 169 | cameraView.setOnFrameListener((data, width, height, rotationDegrees) -> { |
176 | 170 |
|
177 | 171 | long now = System.currentTimeMillis(); |
178 | | - if (now < Preview.this.lastTimestamp + PREVIEW_INTERVAL) |
| 172 | + if (now < CameraViewHolder.this.lastTimestamp + PREVIEW_INTERVAL) |
179 | 173 | return; |
180 | 174 |
|
181 | | - Preview.this.lastTimestamp = now; |
| 175 | + CameraViewHolder.this.lastTimestamp = now; |
182 | 176 |
|
183 | 177 | if (!doingVideoProcessing) { |
184 | 178 |
|
185 | | - Log.i("Preview", "Processing new image"); |
| 179 | + Log.i("CameraViewHolder", "Processing new image"); |
186 | 180 |
|
187 | 181 | mDecodeThreadPool.execute(() -> processNewFrame(data, width, height, rotationDegrees)); |
188 | 182 | } |
@@ -304,7 +298,7 @@ private void processNewFrame (byte[] data, int width, int height, int rotationDe |
304 | 298 |
|
305 | 299 | } catch (Exception e) { |
306 | 300 | // Cannot happen |
307 | | - Log.e("Preview", "error creating image", e); |
| 301 | + Log.e("CameraViewHolder", "error creating image", e); |
308 | 302 | } |
309 | 303 | } |
310 | 304 | } |
@@ -343,19 +337,23 @@ private synchronized boolean recordVideo() { |
343 | 337 | } |
344 | 338 |
|
345 | 339 |
|
346 | | - public void stopCamera () |
| 340 | + public synchronized void stopCamera () |
347 | 341 | { |
348 | 342 | if (cameraView != null) { |
349 | | - // Surface will be destroyed when we return, so stop the preview. |
350 | | - // Because the CameraDevice object is not a shared resource, it's very |
351 | | - // important to release it when the activity is paused. |
352 | | - this.context.unbindService(mConnection); |
353 | | - |
354 | 343 | cameraView.stop(); |
355 | 344 | } |
356 | 345 | } |
357 | 346 |
|
358 | 347 | public int getCameraFacing() { |
359 | 348 | return cameraView.getFacing(); |
360 | 349 | } |
| 350 | + |
| 351 | + public void destroy () |
| 352 | + { |
| 353 | + if (mConnection != null) { |
| 354 | + this.context.unbindService(mConnection); |
| 355 | + mConnection = null; |
| 356 | + } |
| 357 | + stopCamera(); |
| 358 | + } |
361 | 359 | } |
0 commit comments