获取Android手机CPU类型 ARM、ARMV7、NEON

来源:互联网 发布:js点击空白处触发事件 编辑:程序博客网 时间:2024/06/03 19:16

1 查看手机CPU信息

cmd——adb shell——cd /proc------cat cpuinfo


2 获取cpu的是arm指令集,armv7指令集、还是neon指令集

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *  
  3.  * [获取cpu类型和架构] 
  4.  *  
  5.  * @return  
  6.  * 三个参数类型的数组,第一个参数标识是不是ARM架构,第二个参数标识是V6还是V7架构,第三个参数标识是不是neon指令集 
  7.  */  
  8. public static Object[] getCpuArchitecture() {  
  9.     if ((Integer) mArmArchitecture[1] != -1) {  
  10.         return mArmArchitecture;  
  11.     }  
  12.     try {  
  13.         InputStream is = new FileInputStream("/proc/cpuinfo");  
  14.         InputStreamReader ir = new InputStreamReader(is);  
  15.         BufferedReader br = new BufferedReader(ir);  
  16.         try {  
  17.             String nameProcessor = "Processor";  
  18.             String nameFeatures = "Features";  
  19.             String nameModel = "model name";  
  20.             String nameCpuFamily = "cpu family";  
  21.             while (true) {  
  22.                 String line = br.readLine();  
  23.                 String[] pair = null;  
  24.                 if (line == null) {  
  25.                     break;  
  26.                 }  
  27.                 pair = line.split(":");  
  28.                 if (pair.length != 2)  
  29.                     continue;  
  30.                 String key = pair[0].trim();  
  31.                 String val = pair[1].trim();  
  32.                 if (key.compareTo(nameProcessor) == 0) {  
  33.                     String n = "";  
  34.                     for (int i = val.indexOf("ARMv") + 4; i < val.length(); i++) {  
  35.                         String temp = val.charAt(i) + "";  
  36.                         if (temp.matches("\\d")) {  
  37.                             n += temp;  
  38.                         } else {  
  39.                             break;  
  40.                         }  
  41.                     }  
  42.                     mArmArchitecture[0] = "ARM";  
  43.                     mArmArchitecture[1] = Integer.parseInt(n);  
  44.                     continue;  
  45.                 }  
  46.   
  47.                 if (key.compareToIgnoreCase(nameFeatures) == 0) {  
  48.                     if (val.contains("neon")) {  
  49.                         mArmArchitecture[2] = "neon";  
  50.                     }  
  51.                     continue;  
  52.                 }  
  53.   
  54.                 if (key.compareToIgnoreCase(nameModel) == 0) {  
  55.                     if (val.contains("Intel")) {  
  56.                         mArmArchitecture[0] = "INTEL";  
  57.                         mArmArchitecture[2] = "atom";  
  58.                     }  
  59.                     continue;  
  60.                 }  
  61.   
  62.                 if (key.compareToIgnoreCase(nameCpuFamily) == 0) {  
  63.                     mArmArchitecture[1] = Integer.parseInt(val);  
  64.                     continue;  
  65.                 }  
  66.             }  
  67.         } finally {  
  68.             br.close();  
  69.             ir.close();  
  70.             is.close();  
  71.         }  
  72.     } catch (Exception e) {  
  73.         e.printStackTrace();  
  74.     }  
  75.   
  76.     return mArmArchitecture;  
  77. }  

调用的该函数的示例方法

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * 获取FFpeg解码库的名称(如果是插件,会涉及到一个向下兼容的问题,例如:如果当前cpu是V7neo,而又没有neon的解码库,必须要做向下兼容出来 
  3.  * ,如果有V7的库就加载V7的库,有V6的库就加载V6的) 
  4.  */  
  5. public static String getFFmpegLibName(Context context) {  
  6.     if (LIB_FFMPEG_NAME != null) {  
  7.         return LIB_FFMPEG_NAME;  
  8.     }  
  9.     Object[] arch = getCpuArchitecture();  
  10.   
  11.     String libDir = getNativeLibraryDir(context);  
  12.     String libSysDir = "/system/lib";  
  13.   
  14.     if ("ARM".equals(arch[0])) {  
  15.         try {  
  16.             String ffmpeg = String.format("ffmpeg-%d%s", (Integer) arch[1], (String) arch[2]);  
  17.             if (isFileExist(libDir + "/lib" + ffmpeg + ".so") || isFileExist(libSysDir + "/lib" + ffmpeg + ".so")) {  
  18.                 return ffmpeg;  
  19.             } else {  
  20.                 boolean isV7NeonCpu = "neon".equals(arch[2]);  
  21.                 boolean isV7 = ((Integer) arch[1]) == 7 && "".equals(arch[2]);  
  22.                 boolean isV6 = ((Integer) arch[1]) == 6;  
  23.                 if (isV7NeonCpu) {  
  24.                     if (isFileExist(libDir + "/libffmpeg-7neon.so")  
  25.                             || isFileExist(libSysDir + "/libffmpeg-7neon.so")) {  
  26.                         LIB_FFMPEG_NAME = "ffmpeg-7neon";  
  27.                         return "ffmpeg-7neon";  
  28.                     } else if (isFileExist(libDir + "/libffmpeg-7.so")  
  29.                             || isFileExist(libSysDir + "/libffmpeg-7.so")) {  
  30.                         LIB_FFMPEG_NAME = "ffmpeg-7";  
  31.                         return "ffmpeg-7";  
  32.                     } else if (isFileExist(libDir + "/libffmpeg-6.so")  
  33.                             || isFileExist(libSysDir + "/libffmpeg-6.so")) {  
  34.                         LIB_FFMPEG_NAME = "ffmpeg-6";  
  35.                         return "ffmpeg-6";  
  36.                     }  
  37.                 } else if (isV7) {  
  38.                     if (isFileExist(libDir + "/libffmpeg-7.so") || isFileExist(libSysDir + "/libffmpeg-7.so")) {  
  39.                         LIB_FFMPEG_NAME = "ffmpeg-7";  
  40.                         return "ffmpeg-7";  
  41.                     } else if (isFileExist(libDir + "/libffmpeg-6.so")  
  42.                             || isFileExist(libSysDir + "/libffmpeg-6.so")) {  
  43.                         LIB_FFMPEG_NAME = "ffmpeg-6";  
  44.                         return "ffmpeg-6";  
  45.                     }  
  46.                 } else if (isV6) {  
  47.                     if (isFileExist(libDir + "/libffmpeg-6.so") || isFileExist(libSysDir + "/libffmpeg-6.so")) {  
  48.                         LIB_FFMPEG_NAME = "ffmpeg-6";  
  49.                         return "ffmpeg-6";  
  50.                     }  
  51.                 }  
  52.             }  
  53.         } catch (Exception e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.     } else if ("INTEL".equals(arch[0])) {  
  57.         if (isFileExist(libDir + "/libffmpeg-x86atom.so") || isFileExist(libSysDir + "/libffmpeg-x86atom.so")) {  
  58.             LIB_FFMPEG_NAME = "ffmpeg-x86atom";  
  59.             return "ffmpeg-x86atom";  
  60.         }  
  61.     }  
  62.     LIB_FFMPEG_NAME = null;  
  63.     return null;  
  64. }  
0 0