|
5 | 5 | import android.graphics.Rect; |
6 | 6 | import android.graphics.SurfaceTexture; |
7 | 7 | import android.graphics.YuvImage; |
| 8 | +import android.os.Build; |
8 | 9 | import android.os.Bundle; |
9 | 10 | import android.os.Environment; |
10 | 11 | import android.os.Handler; |
|
20 | 21 | import android.widget.Toast; |
21 | 22 |
|
22 | 23 | import com.dji.videostreamdecodingsample.media.DJIVideoStreamDecoder; |
23 | | - |
24 | 24 | import com.dji.videostreamdecodingsample.media.NativeHelper; |
25 | | -import dji.common.camera.SettingsDefinitions; |
26 | | -import dji.common.error.DJIError; |
27 | | -import dji.common.util.CommonCallbacks; |
28 | | -import dji.log.DJILog; |
29 | | -import dji.thirdparty.afinal.core.AsyncTask; |
| 25 | + |
30 | 26 | import java.io.File; |
31 | 27 | import java.io.FileNotFoundException; |
32 | 28 | import java.io.FileOutputStream; |
33 | 29 | import java.io.IOException; |
34 | 30 | import java.io.OutputStream; |
| 31 | +import java.nio.ByteBuffer; |
35 | 32 |
|
| 33 | +import dji.common.camera.SettingsDefinitions; |
| 34 | +import dji.common.error.DJIError; |
36 | 35 | import dji.common.product.Model; |
| 36 | +import dji.common.util.CommonCallbacks; |
37 | 37 | import dji.sdk.base.BaseProduct; |
38 | 38 | import dji.sdk.camera.Camera; |
39 | 39 | import dji.sdk.camera.VideoFeeder; |
40 | 40 | import dji.sdk.codec.DJICodecManager; |
41 | | -import java.nio.ByteBuffer; |
| 41 | +import dji.thirdparty.afinal.core.AsyncTask; |
42 | 42 |
|
43 | 43 | public class MainActivity extends Activity implements DJICodecManager.YuvDataCallback { |
44 | 44 | private static final String TAG = MainActivity.class.getSimpleName(); |
@@ -391,13 +391,18 @@ public void onYuvDataReceived(final ByteBuffer yuvFrame, int dataSize, final int |
391 | 391 | AsyncTask.execute(new Runnable() { |
392 | 392 | @Override |
393 | 393 | public void run() { |
394 | | - saveYuvDataToJPEG(bytes, width, height); |
| 394 | + if (Build.VERSION.SDK_INT <= 23){ |
| 395 | + oldSaveYuvDataToJPEG(bytes, width, height); |
| 396 | + }else { |
| 397 | + newSaveYuvDataToJPEG(bytes, width, height); |
| 398 | + } |
395 | 399 | } |
396 | 400 | }); |
397 | 401 | } |
398 | 402 | } |
399 | 403 |
|
400 | | - private void saveYuvDataToJPEG(byte[] yuvFrame, int width, int height){ |
| 404 | + // For android API <= 23 |
| 405 | + private void oldSaveYuvDataToJPEG(byte[] yuvFrame, int width, int height){ |
401 | 406 | if (yuvFrame.length < width * height) { |
402 | 407 | //DJILog.d(TAG, "yuvFrame size is too small " + yuvFrame.length); |
403 | 408 | return; |
@@ -447,6 +452,26 @@ private void saveYuvDataToJPEG(byte[] yuvFrame, int width, int height){ |
447 | 452 | screenShot(bytes,Environment.getExternalStorageDirectory() + "/DJI_ScreenShot", width, height); |
448 | 453 | } |
449 | 454 |
|
| 455 | + private void newSaveYuvDataToJPEG(byte[] yuvFrame, int width, int height){ |
| 456 | + if (yuvFrame.length < width * height) { |
| 457 | + //DJILog.d(TAG, "yuvFrame size is too small " + yuvFrame.length); |
| 458 | + return; |
| 459 | + } |
| 460 | + int length = width * height; |
| 461 | + |
| 462 | + byte[] u = new byte[width * height / 4]; |
| 463 | + byte[] v = new byte[width * height / 4]; |
| 464 | + for (int i = 0; i < u.length; i++) { |
| 465 | + v[i] = yuvFrame[length + 2 * i]; |
| 466 | + u[i] = yuvFrame[length + 2 * i + 1]; |
| 467 | + } |
| 468 | + for (int i = 0; i < u.length; i++) { |
| 469 | + yuvFrame[length + 2 * i] = u[i]; |
| 470 | + yuvFrame[length + 2 * i + 1] = v[i]; |
| 471 | + } |
| 472 | + screenShot(yuvFrame,Environment.getExternalStorageDirectory() + "/DJI_ScreenShot", width, height); |
| 473 | + } |
| 474 | + |
450 | 475 | /** |
451 | 476 | * Save the buffered data into a JPG image file |
452 | 477 | */ |
|
0 commit comments