免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4133 | 回复: 10

[其他] 尼玛, 累死了, 老子可以宣布是 java 码农了 [复制链接]

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
发表于 2015-06-19 19:23 |显示全部楼层
不知道写的对不对, 还没测试呢, android 的代码写的太费劲了; 各种鸟限制
  1. class video_message_handler extends Handler {
  2.     private video_cap_thread obj = null;
  3.     public video_message_handler(video_cap_thread obj) {
  4.         this.obj = obj;
  5.     }

  6.     public void handleMessage(Message msg) {
  7.         int n = 0;
  8.         switch (msg.what) {
  9.             case 0: obj.timer_expires(this); break;
  10.             default: Log.i("zylthinking", "impossible cmd " + msg.what + " issued"); break;
  11.         }
  12.     }
  13. }

  14. class video_cap_thread extends Thread implements SurfaceTexture.OnFrameAvailableListener, Camera.PreviewCallback {
  15.     private static EGL11 egl = null;
  16.     private static EGLDisplay display = null;
  17.     private static EGLSurface surface = null;
  18.     private static EGLContext context = null;
  19.     private static int texname = egl_initialize(0);
  20.     private SurfaceTexture texture = null;

  21.     public int width = 0;
  22.     public int height = 0;
  23.     private int fps = 0;
  24.     private long something = -1;

  25.     private boolean running = false;
  26.     private Camera camera = null;
  27.     private Semaphore sem = null;

  28.     public video_cap_thread(int width, int height, int fps, long something) {
  29.         this.with = with;
  30.         this.height = height;
  31.         this.fps = fps * 1000;
  32.         this.something = something;
  33.         this.sem = new Semaphore(1);
  34.         sem.acquire(1);
  35.     }

  36.     private int start_camera() {
  37.         camera = Camera.open(0);
  38.         Camera.Parameters params = camera.getParameters();
  39.         List<Camera.Size> sizes = params.getSupportedPictureSizes();

  40.         int[] intp = {0, 0};
  41.         find_picture_size(list, this.width, this.height, intp);
  42.         if (intp[0] == 0 || intp[1] == 0) {
  43.             camera.release();
  44.             camera = null;
  45.             return -1;
  46.         }
  47.         params.setPictureSize(intp[0], intp[1]);

  48.         List<int[]> range = params.getSupportedPreviewFpsRange();
  49.         int selected_fps = find_fps(range, this.fps);
  50.         if (selected_fps == 0) {
  51.             camera.release();
  52.             camera = null;
  53.             return -1;
  54.         }
  55.         params.setPreviewFpsRange(selected_fps, selected_fps);

  56.         params.set("orientation", "portrait");
  57.         params.set("rotation", 90);
  58.         camera.setParameters(params);

  59.         if (-1 == reinit_camera(false)) {
  60.             camera.release();
  61.             camera = null;
  62.             return -1;
  63.         }

  64.         int nb = intp[0] * intp[1] * ImageFormat.getBitsPerPixel(ImageFormat.NV21) / 8;
  65.         camera.addCallbackBuffer(new byte[nb]);
  66.         camera.addCallbackBuffer(new byte[nb]);
  67.         camera.addCallbackBuffer(new byte[nb]);
  68.         camera.addCallbackBuffer(new byte[nb]);
  69.         camera.setPreviewCallbackWithBuffer(this);
  70.         camera.startPreview();
  71.         running = true;
  72.         return 0;
  73.     }

  74.     public void onPreviewFrame (byte[] data, Camera camera) {
  75.         int n = mmapi.video_push(something, data);
  76.         if (n == -1) {
  77.             Looper.quit();
  78.         } else {
  79.             camera.addCallbackBuffer(data);
  80.         }
  81.     }

  82.     public void onFrameAvailable(SurfaceTexture texture) {
  83.         texture.updateTexImage();
  84.         long stamp = texture.getTimestamp();
  85.     }

  86.     private int reinit_camera(boolean need_run) {
  87.         if (running) {
  88.             camera.stopPreview();
  89.             running = false;
  90.             if (texture != null) {
  91.                 texture->release();
  92.                 texture = null;
  93.             }

  94.             if (texname != 0) {
  95.                 int[] intp = {texname};
  96.                 glDeleteTextures(1, texname, 0);
  97.                 texname = 0;
  98.             }

  99.             if (context != null) {
  100.                 egl.eglDestroyContext(display, context);
  101.                 context = null;
  102.             }
  103.         }

  104.         texname = egl_initialize(texname);
  105.         if (texname == 0) {
  106.             return -1;
  107.         }

  108.         Camera.Parameters params = camera.getParameters();
  109.         try {
  110.             texture = new SurfaceTexture(texname);
  111.             camera.setPreviewTexture(texture);
  112.         } catch (Exception e) {
  113.             texture = null;
  114.             return -1;
  115.         }

  116.         texture.setOnFrameAvailableListener(this);
  117.         if (need_run) {
  118.             camera.startPreview();
  119.             running = true;
  120.         }
  121.         return 0;
  122.     }

  123.     public boolean camera_start_failed() {
  124.         sem.acquire(1);
  125.         sem = null;
  126.         return !this.running;
  127.     }

  128.     public void timer_expires(video_message_handler handler) {
  129.         if (!egl.eglMakeCurrent(display, surface, surface, context)) {
  130.             int n = egl.eglGetError();
  131.             if (n == EGL11.EGL_CONTEXT_LOST) {
  132.                 reinit_camera(true);
  133.             } else {
  134.                 Log.i("zylthinking", "eglMakeCurrent failed with " + n);
  135.             }
  136.         }
  137.         handler.sendEmptyMessageDelayed(0, 1000);
  138.     }

  139.     public void run() {
  140.         Looper.prepare();

  141.         video_message_handler handler = new video_message_handler(this);
  142.         if (!handler.sendEmptyMessageDelayed(0, 1000)) {
  143.             sem.release();
  144.             sem = null;
  145.             return;
  146.         }

  147.         int n = obj.start_camera();
  148.         sem.release();
  149.         sem = null;
  150.         if (-1 == n) {
  151.             return;
  152.         }

  153.         Looper.loop();
  154.         handler = null;

  155.         if (running) {
  156.             camera.stopPreview();
  157.             if (texture != null) {
  158.                 texture->release();
  159.                 texture = null;
  160.             }
  161.             running = false;
  162.         }
  163.     }

  164.     private int find_fps(List<int[]> range, int fps) {
  165.         int nearest = 0;

  166.         for (int i = 0; i < range.size(); ++i) {
  167.             int[] r = range.get(i);
  168.             if (r[0] <= fps && r[1] >= fps) {
  169.                 return fps;
  170.             }

  171.             int differ = nearest - fps;
  172.             if (r[0] > fps) {
  173.                 int n = r[0] - fps;
  174.                 if (n * n < differ * differ) {
  175.                     nearest = r[0];
  176.                 }
  177.             } else {
  178.                 int n = r[1] - fps;
  179.                 if (n * n < differ * differ) {
  180.                     nearest = r[1];
  181.                 }
  182.             }
  183.         }
  184.         return nearest;
  185.     }

  186.     private void find_picture_size(List<Camera.Size> list, int width, int height, int[] dim) {
  187.         int i;
  188.         for (i = 0; i < list.size(); ++i) {
  189.             Camera.Size size = (Size) list.get(i);
  190.             if (size.width == width && size.height == height) {
  191.                 dim[0] = width;
  192.                 dim[1] = height;
  193.                 return;
  194.             }
  195.         }

  196.         int w1 = Integer.MAX_VALUE;
  197.         int h1 = Integer.MAX_VALUE;
  198.         for (i = 0; i < list.size(); ++i) {
  199.             Camera.Size size = (Size) list.get(i);
  200.             if (size.width >= width && size.height >= height) {
  201.                 if (w1 >= size.width && h1 >= size.height) {
  202.                     w1 = size.width;
  203.                     h1 = size.height;
  204.                     continue;
  205.                 }

  206.                 if (w1 * h1 > size.width * size.height) {
  207.                     w1 = size.width;
  208.                     h1 = size.height;
  209.                     continue;
  210.                 }
  211.             }
  212.         }

  213.         if (w1 != Integer.MAX_VALUE && h1 != Integer.MAX_VALUE) {
  214.             dim[0] = w1;
  215.             dim[1] = h1;
  216.             return;
  217.         }

  218.         w1 = 0;
  219.         h1 = 0;
  220.         int w2 = 0;
  221.         int h2 = 0;
  222.         int w3 = 0;
  223.         int h3 = 0;
  224.         for (i = 0; i < list.size(); ++i) {
  225.             Camera.Size size = (Size) list.get(i);
  226.             if (size.width >= width) {
  227.                 if (size.height > h1) {
  228.                     w1 = size.width;
  229.                     h1 = size.height;
  230.                     continue;
  231.                 }
  232.             } else if (size.height >= height) {
  233.                 if (size.width > w2) {
  234.                     w2 = size.width;
  235.                     h2 = size.height;
  236.                     continue;
  237.                 }
  238.             } else {
  239.                 if (w3 * h3 < size.width * size.height) {
  240.                     w3 = size.width;
  241.                     h3 = size.height;
  242.                 }
  243.             }
  244.         }

  245.         if (width * h1 > w2 * height) {
  246.             dim[0] = w1;
  247.             dim[1] = h1;
  248.         } else if (w2 * height > width * h1) {
  249.             dim[0] = w2;
  250.             dim[1] = h2;
  251.         } else {
  252.             dim[0] = w3;
  253.             dim[1] = h3;
  254.         }
  255.     }

  256.     private static int egl_initialize(int name) {
  257.         if (name != 0) {
  258.             return 0;
  259.         }

  260.         if (egl == null) {
  261.             egl = (EGL11) EGLContext.getEGL();
  262.             if (egl == null) {
  263.                 return 0;
  264.             }
  265.         }

  266.         if (display == null) {
  267.             display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
  268.             if (display == null || display == EGL10.EGL_NO_DISPLAY) {
  269.                 display = null
  270.                 return 0;
  271.             }
  272.         }

  273.         // reinitialize is allowed by egl spec
  274.         if (!egl.eglInitialize(display, null)) {
  275.             return 0;
  276.         }

  277.         int[] attr = {
  278.             EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
  279.             EGL10.EGL_NONE
  280.         };

  281.         EGLConfig[] conf = new EGLConfig[1];
  282.         if (!egl.eglChooseConfig(display, attr, conf, 1, new int[1])) {
  283.             return 0;
  284.         }

  285.         if (surface == null) {
  286.             int[] surface_attr = {
  287.                 EGL10.EGL_WIDTH, 32,
  288.                 EGL10.EGL_HEIGHT, 32,
  289.                 EGL10.EGL_NONE
  290.             };

  291.             surface = egl.eglCreatePbufferSurface(display, conf, surface_attr);
  292.             if (surface == EGL10.EGL_NO_SURFACE) {
  293.                 return 0;
  294.             }
  295.         }

  296.         if (context == null) {
  297.             attr[0] = 0x3098;
  298.             attr[1] = 2;
  299.             attr[2] = GL10.EGL_NONE;

  300.             context = egl.eglCreateContext(display, conf, EGL10.EGL_NO_CONTEXT, attr);
  301.             if (context == null || context == EGL10.EGL_NO_CONTEXT) {
  302.                 context = null;
  303.                 return 0;
  304.             }
  305.         }

  306.         int intp[] = new int[1];
  307.         GLES20.glGenTextures(1, intp, 0);
  308.         return intp[0];
  309.     }
  310. }
复制代码

论坛徽章:
89
水瓶座
日期:2014-04-01 08:53:31天蝎座
日期:2014-04-01 08:53:53天秤座
日期:2014-04-01 08:54:02射手座
日期:2014-04-01 08:54:15子鼠
日期:2014-04-01 08:55:35辰龙
日期:2014-04-01 08:56:36未羊
日期:2014-04-01 08:56:27戌狗
日期:2014-04-01 08:56:13亥猪
日期:2014-04-01 08:56:02亥猪
日期:2014-04-08 08:38:58程序设计版块每日发帖之星
日期:2016-01-05 06:20:00程序设计版块每日发帖之星
日期:2016-01-07 06:20:00
发表于 2015-06-19 19:58 |显示全部楼层
据说java随便搞搞都能弄出很多行来。

论坛徽章:
36
子鼠
日期:2013-08-28 22:23:29黄金圣斗士
日期:2015-12-01 11:37:51程序设计版块每日发帖之星
日期:2015-12-14 06:20:00CU十四周年纪念徽章
日期:2015-12-22 16:50:40IT运维版块每日发帖之星
日期:2016-01-25 06:20:0015-16赛季CBA联赛之深圳
日期:2016-01-27 10:31:172016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之福建
日期:2016-04-07 11:25:2215-16赛季CBA联赛之青岛
日期:2016-04-29 18:02:5915-16赛季CBA联赛之北控
日期:2016-06-20 17:38:50技术图书徽章
日期:2016-07-19 13:54:03程序设计版块每日发帖之星
日期:2016-08-21 06:20:00
发表于 2015-06-19 19:59 |显示全部楼层
楼主你为何这么屌!

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2015-06-20 07:57 |显示全部楼层

楼主你为何这么屌!

论坛徽章:
0
发表于 2015-06-21 18:20 |显示全部楼层
楼主如今这般牛逼,你家人造吗,

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
发表于 2015-06-24 10:47 |显示全部楼层
我也想入java,感觉语法都掌握不来,楼主是直接上手搞吧。

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
发表于 2015-06-25 12:28 |显示全部楼层
回复 6# linux_c_py_php

是的, 没怎么看书, 凭直觉上, 反正如果不行, 那肯定编译通不过

   

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
发表于 2015-06-25 13:30 |显示全部楼层
杯 具 了
聊聊java吧,楼主同志

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
发表于 2015-06-25 13:30 |显示全部楼层
杯 具 了
聊聊java吧,楼主同志

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
发表于 2015-06-25 16:26 |显示全部楼层
在加几行, 看看是不是 c 式 java? 我觉得挺好

  1. package libmm;
  2. import android.view.Surface;
  3. import android.util.Log;
  4. import java.util.LinedList;
  5. import java.util.concurrent.locks.ReentrantLock;

  6. class pool_item {
  7.     Surface sfc;
  8.     long handle;
  9. }

  10. public final class surfacepool
  11. {
  12.     private static LinkedList<pool_item> pool = new LinedList<pool_item>();
  13.     private static ReentrantLock lck = new ReentrantLock();

  14.     public static pool_item add_surface(Surface sfc, int r, int g, int b) {
  15.         pool_item item = new pool_item();
  16.         item.sfc = sfc;
  17.         item.handle = surface_register(r, g, b, sfc);

  18.         if (item.handle == 0) {
  19.             return null;
  20.         }

  21.         lck.lock();
  22.         pool.add(item);
  23.         lck.unlock();
  24.         return item;
  25.     }

  26.     public static void remove_surface(Surface sfc) {
  27.         pool_item item = null;

  28.         lck.lock();
  29.         for (int i = 0; i < pool.size(); ++i) {
  30.             pool_item entry = pool.get(i);
  31.             if (entry.sfc == sfc) {
  32.                 item = entry;
  33.                 pool.remove(i);
  34.             }
  35.         }
  36.         lck.unlock();

  37.         if (item != null) {
  38.             surface_unregister(item.handle);
  39.         }
  40.     }

  41.     private static final long[] area_open(long id) {
  42.         long[] xywhz = new long[6];
  43.         pool_item item = null;
  44.         lck.lock();

  45.         try {
  46.             item = area_open(id, xywhz);
  47.         } catch (Throwable t) {
  48.             item = null;
  49.         } finally {
  50.             lck.unlock();
  51.         }

  52.         if (item == null) {
  53.             return null;
  54.         }
  55.         xywhz[5] = handle_reference(item.handle);
  56.         return xywhz;
  57.     }

  58.     private final static pool_item area_open(long id, long[] xywhz) throws Exception {
  59.         Log.i("zylthinking", "you must implement this method to return a Surface for video " + id);
  60.         Log.i("zylthinking", "xywh means (x, y, width, height) which will be the displaying area in the surface");
  61.         Log.i("zylthinking", "and z means z coordinate, must be 0 or 1, 1 is nearer to your eye");
  62.         Log.i("zylthinking", "you can select an existing Surface or create a new one");
  63.         Log.i("zylthinking", "notice: it is a ReentrantLock, meaning you can call add_surface here");
  64.         return null;
  65.     }

  66.     private static native long handle_reference(long handle);
  67.     private static native long surface_register(int r, int g, int b, Surface sfc);
  68.     private static native void surface_unregister(long handle);
  69.     static {
  70.         System.loadLibrary("mm");
  71.     }
  72. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP