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

1 查看手机CPU信息

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


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

	/**
	 * 
	 * [获取cpu类型和架构]
	 * 
	 * @return 
	 * 三个参数类型的数组,第一个参数标识是不是ARM架构,第二个参数标识是V6还是V7架构,第三个参数标识是不是neon指令集
	 */
	public static Object[] getCpuArchitecture() {
		if ((Integer) mArmArchitecture[1] != -1) {
			return mArmArchitecture;
		}
		try {
			InputStream is = new FileInputStream("/proc/cpuinfo");
			InputStreamReader ir = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(ir);
			try {
				String nameProcessor = "Processor";
				String nameFeatures = "Features";
				String nameModel = "model name";
				String nameCpuFamily = "cpu family";
				while (true) {
					String line = br.readLine();
					String[] pair = null;
					if (line == null) {
						break;
					}
					pair = line.split(":");
					if (pair.length != 2)
						continue;
					String key = pair[0].trim();
					String val = pair[1].trim();
					if (key.compareTo(nameProcessor) == 0) {
						String n = "";
						for (int i = val.indexOf("ARMv") + 4; i < val.length(); i++) {
							String temp = val.charAt(i) + "";
							if (temp.matches("\\d")) {
								n += temp;
							} else {
								break;
							}
						}
						mArmArchitecture[0] = "ARM";
						mArmArchitecture[1] = Integer.parseInt(n);
						continue;
					}

					if (key.compareToIgnoreCase(nameFeatures) == 0) {
						if (val.contains("neon")) {
							mArmArchitecture[2] = "neon";
						}
						continue;
					}

					if (key.compareToIgnoreCase(nameModel) == 0) {
						if (val.contains("Intel")) {
							mArmArchitecture[0] = "INTEL";
							mArmArchitecture[2] = "atom";
						}
						continue;
					}

					if (key.compareToIgnoreCase(nameCpuFamily) == 0) {
						mArmArchitecture[1] = Integer.parseInt(val);
						continue;
					}
				}
			} finally {
				br.close();
				ir.close();
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mArmArchitecture;
	}

调用的该函数的示例方法

	/*
	 * 获取FFpeg解码库的名称(如果是插件,会涉及到一个向下兼容的问题,例如:如果当前cpu是V7neo,而又没有neon的解码库,必须要做向下兼容出来
	 * ,如果有V7的库就加载V7的库,有V6的库就加载V6的)
	 */
	public static String getFFmpegLibName(Context context) {
		if (LIB_FFMPEG_NAME != null) {
			return LIB_FFMPEG_NAME;
		}
		Object[] arch = getCpuArchitecture();

		String libDir = getNativeLibraryDir(context);
		String libSysDir = "/system/lib";

		if ("ARM".equals(arch[0])) {
			try {
				String ffmpeg = String.format("ffmpeg-%d%s", (Integer) arch[1], (String) arch[2]);
				if (isFileExist(libDir + "/lib" + ffmpeg + ".so") || isFileExist(libSysDir + "/lib" + ffmpeg + ".so")) {
					return ffmpeg;
				} else {
					boolean isV7NeonCpu = "neon".equals(arch[2]);
					boolean isV7 = ((Integer) arch[1]) == 7 && "".equals(arch[2]);
					boolean isV6 = ((Integer) arch[1]) == 6;
					if (isV7NeonCpu) {
						if (isFileExist(libDir + "/libffmpeg-7neon.so")
								|| isFileExist(libSysDir + "/libffmpeg-7neon.so")) {
							LIB_FFMPEG_NAME = "ffmpeg-7neon";
							return "ffmpeg-7neon";
						} else if (isFileExist(libDir + "/libffmpeg-7.so")
								|| isFileExist(libSysDir + "/libffmpeg-7.so")) {
							LIB_FFMPEG_NAME = "ffmpeg-7";
							return "ffmpeg-7";
						} else if (isFileExist(libDir + "/libffmpeg-6.so")
								|| isFileExist(libSysDir + "/libffmpeg-6.so")) {
							LIB_FFMPEG_NAME = "ffmpeg-6";
							return "ffmpeg-6";
						}
					} else if (isV7) {
						if (isFileExist(libDir + "/libffmpeg-7.so") || isFileExist(libSysDir + "/libffmpeg-7.so")) {
							LIB_FFMPEG_NAME = "ffmpeg-7";
							return "ffmpeg-7";
						} else if (isFileExist(libDir + "/libffmpeg-6.so")
								|| isFileExist(libSysDir + "/libffmpeg-6.so")) {
							LIB_FFMPEG_NAME = "ffmpeg-6";
							return "ffmpeg-6";
						}
					} else if (isV6) {
						if (isFileExist(libDir + "/libffmpeg-6.so") || isFileExist(libSysDir + "/libffmpeg-6.so")) {
							LIB_FFMPEG_NAME = "ffmpeg-6";
							return "ffmpeg-6";
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if ("INTEL".equals(arch[0])) {
			if (isFileExist(libDir + "/libffmpeg-x86atom.so") || isFileExist(libSysDir + "/libffmpeg-x86atom.so")) {
				LIB_FFMPEG_NAME = "ffmpeg-x86atom";
				return "ffmpeg-x86atom";
			}
		}
		LIB_FFMPEG_NAME = null;
		return null;
	}


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值