Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. // The callback for receiving the raw H264 video data for camera live view
  2. mReceivedVideoDataListener = new VideoFeeder.VideoDataListener() {
  3. @Override
  4. public void onReceive(byte[] videoBuffer, int size) {
  5. //Log.d("BytesReceived", Integer.toString(videoStreamFrameNumber));
  6. if (videoStreamFrameNumber++%30 == 0){
  7. //convert video buffer to opencv array
  8. OpenCvAndModelAsync openCvAndModelAsync = new OpenCvAndModelAsync();
  9. openCvAndModelAsync.execute(videoBuffer);
  10. }
  11. if (mCodecManager != null) {
  12. mCodecManager.sendDataToDecoder(videoBuffer, size);
  13. }
  14. }
  15. };
  16.  
  17. @Override
  18. public void onYuvDataReceived(final ByteBuffer yuvFrame, int dataSize, final int width, final int height) {
  19. //In this demo, we test the YUV data by saving it into JPG files.
  20. //DJILog.d(TAG, "onYuvDataReceived " + dataSize);
  21. if (count++ % 30 == 0 && yuvFrame != null) {
  22. final byte[] bytes = new byte[dataSize];
  23. yuvFrame.get(bytes);
  24. AsyncTask.execute(new Runnable() {
  25. @Override
  26. public void run() {
  27. if (bytes.length >= width * height) {
  28. Log.d("MatWidth", "Made it");
  29. YuvImage yuvImage = saveYuvDataToJPEG(bytes, width, height);
  30. Bitmap rgbYuvConvert = convertYuvImageToRgb(yuvImage, width, height);
  31.  
  32. Mat yuvMat = new Mat(height, width, CvType.CV_8UC1);
  33. yuvMat.put(0, 0, bytes);
  34. //OpenCv Stuff
  35. }
  36. }
  37. });
  38. }
  39. }
  40.  
  41. /* Async OpenCV Code */
  42. private class OpenCvAndModelAsync extends AsyncTask<byte[], Void, double[]> {
  43. @Override
  44. protected double[] doInBackground(byte[]... params) {//Background Code Executing. Don't touch any UI components
  45. //get fpv feed and convert bytes to mat array
  46. Mat videoBufMat = new Mat(4, params[0].length, CvType.CV_8UC4);
  47. videoBufMat.put(0,0, params[0]);
  48. //if I add this in it says the bytes are empty.
  49. //Mat videoBufMat = Imgcodecs.imdecode(encodeVideoBuf, Imgcodecs.IMREAD_ANYCOLOR);
  50. //encodeVideoBuf.release();
  51. Log.d("MatRgba", videoBufMat.toString());
  52. for (int i = 0; i< videoBufMat.rows(); i++){
  53. for (int j=0; j< videoBufMat.cols(); j++){
  54. double[] rgb = videoBufMat.get(i, j);
  55. Log.i("Matrix", "red: "+rgb[0]+" green: "+rgb[1]+" blue: "+rgb[2]+" alpha: "
  56. + rgb[3] + " Length: " + rgb.length + " Rows: "
  57. + videoBufMat.rows() + " Columns: " + videoBufMat.cols());
  58. }
  59. }
  60. double[] center = openCVThingy(videoBufMat);
  61. return center;
  62. }
  63. protected void onPostExecute(double[] center) {
  64. //handle ui or another async task if necessary
  65. }
  66. }
  67.  
  68. 2019-05-23 21:14:29.601 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 2425
  69. 2019-05-23 21:14:29.802 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 2659
  70. 2019-05-23 21:14:30.004 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 6
  71. 2019-05-23 21:14:30.263 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 6015
  72. 2019-05-23 21:14:30.507 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 6
  73. 2019-05-23 21:14:30.766 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 4682
  74. 2019-05-23 21:14:31.005 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 6
  75. 2019-05-23 21:14:31.234 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 2840
  76. 2019-05-23 21:14:31.433 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 4482
  77. 2019-05-23 21:14:31.664 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 6
  78. 2019-05-23 21:14:31.927 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 4768
  79. 2019-05-23 21:14:32.174 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 6
  80. 2019-05-23 21:14:32.433 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 4700
  81. 2019-05-23 21:14:32.668 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 6
  82. 2019-05-23 21:14:32.864 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 4740
  83. 2019-05-23 21:14:33.102 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 6
  84. 2019-05-23 21:14:33.365 21431-22086/com.dji.simulatorDemo D/VideoBufferSize: 4640
  85.  
  86. Mat videoBufMat = Imgcodecs.imdecode(new MatOfByte(params[0]), Imgcodecs.IMREAD_UNCHANGED);
  87.  
  88. Mat encodeVideoBuf = new Mat(4, params[0].length, CvType.CV_8UC4);
  89. encodeVideoBuf.put(0,0, params[0]);
  90. Mat videoBufMat = Imgcodecs.imdecode(encodeVideoBuf, Imgcodecs.IMREAD_UNCHANGED);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement