New file |
| | |
| | | package safeluck.drive.evaluation.util; |
| | | |
| | | import android.app.Activity; |
| | | import android.content.Context; |
| | | import android.content.res.Resources; |
| | | import android.graphics.Bitmap; |
| | | import android.telephony.TelephonyManager; |
| | | import android.util.DisplayMetrics; |
| | | import android.view.Surface; |
| | | |
| | | import com.google.zxing.BarcodeFormat; |
| | | import com.google.zxing.EncodeHintType; |
| | | import com.google.zxing.MultiFormatWriter; |
| | | import com.google.zxing.WriterException; |
| | | import com.google.zxing.common.BitMatrix; |
| | | |
| | | import java.util.Hashtable; |
| | | |
| | | import static android.graphics.Color.BLACK; |
| | | |
| | | |
| | | public final class DimenUtil { |
| | | |
| | | public static int getScreenWidth(Context context ) { |
| | | final Resources resources = context.getResources(); |
| | | final DisplayMetrics dm = resources.getDisplayMetrics(); |
| | | return dm.widthPixels; |
| | | } |
| | | |
| | | public static int getScreenHeight(Context context) { |
| | | final Resources resources = context.getResources(); |
| | | final DisplayMetrics dm = resources.getDisplayMetrics(); |
| | | return dm.heightPixels; |
| | | } |
| | | |
| | | public static int getDisplayRotation(Activity activity) { |
| | | int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); |
| | | switch (rotation) { |
| | | case Surface.ROTATION_0: |
| | | return 0; |
| | | case Surface.ROTATION_90: |
| | | return 90; |
| | | case Surface.ROTATION_180: |
| | | return 180; |
| | | case Surface.ROTATION_270: |
| | | return 270; |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | public static Bitmap createQRCode(String str, int widthAndHeight) |
| | | throws WriterException { |
| | | Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); |
| | | hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); |
| | | BitMatrix matrix = new MultiFormatWriter().encode(str, |
| | | BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); |
| | | |
| | | int width = matrix.getWidth(); |
| | | int height = matrix.getHeight(); |
| | | int[] pixels = new int[width * height]; |
| | | //画黑点 |
| | | for (int y = 0; y < height; y++) { |
| | | for (int x = 0; x < width; x++) { |
| | | if (matrix.get(x, y)) { |
| | | pixels[y * width + x] = BLACK; //0xff000000 |
| | | } |
| | | } |
| | | } |
| | | Bitmap bitmap = Bitmap.createBitmap(width, height, |
| | | Bitmap.Config.ARGB_8888); |
| | | bitmap.setPixels(pixels, 0, width, 0, 0, width, height); |
| | | return bitmap; |
| | | } |
| | | |
| | | } |