package com.uiui.zyos.utils; import android.annotation.SuppressLint; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.os.BatteryManager; import android.os.Build; import android.os.Environment; import android.os.PowerManager; import android.util.DisplayMetrics; import android.util.Log; import android.view.WindowManager; import androidx.core.content.ContextCompat; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.uiui.zyos.BuildConfig; import com.uiui.zyos.R; import java.io.File; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class Utils { private static final String TAG = Utils.class.getSimpleName(); /** * 获取设备序列号 * // * @return // */ // @SuppressLint("MissingPermission") // public static String getSerial() { // String serial = "unknow"; // try { // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {//9.0+ // serial = Build.getSerial(); // } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {//8.0+ // serial = Build.SERIAL; // } else {//8.0- // Class c = Class.forName("android.os.SystemProperties"); // Method get = c.getMethod("get", String.class); // serial = (String) get.invoke(c, "ro.serialno"); // } // } catch (Exception e) { // e.printStackTrace(); // Log.e("e", "读取设备序列号异常:" + e.toString()); // } // if (BuildConfig.DEBUG) { //// return "QNG2DKB00463"; // } // return serial; // } public static String getAndroiodScreenProperty(Context context) { Log.e("getAndroiodScreenProperty", "heightPixels:" + context.getResources().getDisplayMetrics().heightPixels); Log.e("getAndroiodScreenProperty", "widthPixels:" + context.getResources().getDisplayMetrics().widthPixels); Log.e("getAndroiodScreenProperty", "density:" + context.getResources().getDisplayMetrics().density); Log.e("getAndroiodScreenProperty", "densityDpi:" + context.getResources().getDisplayMetrics().densityDpi); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getRealMetrics(dm); int width = dm.widthPixels; // 屏幕宽度(像素) int height = dm.heightPixels; // 屏幕高度(像素) float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5) int densityDpi = dm.densityDpi; // 屏幕密度dpi(120 / 160 / 240) // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度 int screenWidth = (int) (width / density); // 屏幕宽度(dp) int screenHeight = (int) (height / density);// 屏幕高度(dp) Log.e("getAndroiodScreenProperty", "屏幕宽度(像素):" + width); Log.e("getAndroiodScreenProperty", "屏幕高度(像素):" + height); Log.e("getAndroiodScreenProperty", "屏幕密度(0.75 / 1.0 / 1.5):" + density); Log.e("getAndroiodScreenProperty", "屏幕密度dpi(120 / 160 / 240):" + densityDpi); Log.e("getAndroiodScreenProperty", "屏幕宽度(dp):" + screenWidth); Log.e("getAndroiodScreenProperty", "屏幕高度(dp):" + screenHeight); return width + "×" + height; } public static String getDeviceSN() { String serial = null; try { Class c = Class.forName("android.os.SystemProperties"); Method get = c.getMethod("get", String.class); serial = (String) get.invoke(c, "persist.sys.hrSerial"); } catch (Exception e) { e.printStackTrace(); } return serial; } /** * 获取电量 * * @param mContext * @return */ synchronized public static int getBatteryLevel(Context mContext) { if (Build.VERSION.SDK_INT >= 21) { return ((BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE)).getIntProperty(4); } else { Intent intent = (new ContextWrapper(mContext)).registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED")); return intent.getIntExtra("level", -1) * 100 / intent.getIntExtra("scale", -1); } } public static String getDownLoadPath(Context context) { String path = ContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DOWNLOADS)[0].getAbsolutePath(); return path + File.separator; } public static String getFileNamefromURL(String url) { int position = url.lastIndexOf("/"); return url.substring(position + 1); } public static Bitmap getRoundedBitmap(Bitmap mBitmap, Context context) { Bitmap bgBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888); Bitmap mask = BitmapFactory.decodeResource(context.getResources(), R.drawable.mask); int width = mask.getWidth(); int height = mask.getHeight(); Bitmap bitmapScale = Bitmap.createScaledBitmap(mBitmap, width, height, true); bitmapScale.setDensity(context.getResources().getDisplayMetrics().densityDpi); // Palette p = Palette.from(mBitmap).generate(); // Palette.Swatch vibrant = p.getVibrantSwatch();//有活力的 // int color = vibrant.getRgb(); //样本中的像素数量 Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(); Paint paint = new Paint(); canvas.setBitmap(result); // canvas.drawColor(color); canvas.drawBitmap(mask, 0, 0, paint); // paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmapScale, 0, 0, paint); // return result; Bitmap result2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas2 = new Canvas(); Paint paint2 = new Paint(); canvas2.setBitmap(result2); canvas2.drawBitmap(mask, 0, 0, paint2); paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas2.drawBitmap(result, 0, 0, paint2); return result2; // Canvas mCanvas = new Canvas(); // mCanvas.setBitmap(bgBitmap); // Paint mPaint = new Paint(); // RectF mRectM = new RectF(scaleM, scaleM, mBitmap.getWidth() - scaleM, mBitmap.getHeight() - scaleM); //设置剪裁圆角的区域 // Rect mRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); // RectF mRectF = mRectM; // // float roundPx = 15; //圆角半径 // mPaint.setAntiAlias(true); // //Log.d("wy"+TAG,"mBitmap.getWidth()="+mBitmap.getWidth()+", mBitmap.getHeight()="+mBitmap.getHeight()); // mCanvas.drawRoundRect(mRectF, roundPx, roundPx, mPaint); // mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // mCanvas.drawBitmap(mBitmap, mRect, mRect, mPaint); // return bgBitmap; } public static Bitmap createQRImage(String content, int widthPix, int heightPix) { try { // if (content == null || "".equals(content)) { // return false; // } //配置参数 Map hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //容错级别 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //设置空白边距的宽度 hints.put(EncodeHintType.MARGIN, 1); //default is 4 // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = null; try { bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints); } catch (WriterException e) { e.printStackTrace(); } int[] pixels = new int[widthPix * heightPix]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < heightPix; y++) { for (int x = 0; x < widthPix; x++) { if (bitMatrix.get(x, y)) { pixels[y * widthPix + x] = 0xff0480ff; } else { pixels[y * widthPix + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix); // // if (logoBm != null) { // bitmap = addLogo(bitmap, logoBm); // } //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的, // 内存消耗巨大! return bitmap; // return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100); } catch (Exception e) { e.printStackTrace(); } return null; } public static boolean isScreenOn(Context context) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); //true为打开,false为关闭 return powerManager.isInteractive(); } }