This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programming_tips_and_tricks [2015/11/02 05:13] – [Java & C#] ledyx | programming_tips_and_tricks [2021/02/07 03:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| = 공통 = | = 공통 = | ||
| + | |||
| + | == 한글 자동완성 == | ||
| + | https:// | ||
| == Polymorphism(다형성) == | == Polymorphism(다형성) == | ||
| Line 195: | Line 198: | ||
| int sec = (int)(decimalPart2 * 60); | int sec = (int)(decimalPart2 * 60); | ||
| - | return string.Format(" | + | return string.Format(" |
| } | } | ||
| Line 249: | Line 252: | ||
| { | { | ||
| return (double)deg + ((double)min / 60) + ((double)sec / 3600); | return (double)deg + ((double)min / 60) + ((double)sec / 3600); | ||
| + | } | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | /* 신규 추가 */ | ||
| + | | ||
| + | // < | ||
| + | /// Degree → 도분초 (단위 포함) | ||
| + | /// (-122.333333 → 122:20:00 W) | ||
| + | /// </ | ||
| + | public static string Degree2DMS(double degree) | ||
| + | { | ||
| + | int deg = (int)Math.Abs(degree); | ||
| + | |||
| + | double min_temp = (Math.Abs(degree) - deg) * 60; | ||
| + | double min_decPart = min_temp - (int)min_temp; | ||
| + | int min = min_decPart >= 0.999 ? (int)Math.Round(min_temp, | ||
| + | |||
| + | double sec_temp = (Math.Abs(degree) - deg - ((double)min / (double)60)) * 3600; | ||
| + | double sec_decPart = sec_temp - (int)sec_temp; | ||
| + | int sec = sec_decPart >= 0.999 ? (int)Math.Round(sec_temp, | ||
| + | |||
| + | |||
| + | |||
| + | string unit = string.Empty; | ||
| + | string fmt = string.Empty; | ||
| + | if (deg <= 90) | ||
| + | { | ||
| + | fmt = " | ||
| + | unit = degree >= 0 ? " N" : " S"; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | fmt = " | ||
| + | unit = degree >= 0 ? " E" : " W"; | ||
| + | } | ||
| + | |||
| + | return string.Format(" | ||
| + | } | ||
| + | |||
| + | /// < | ||
| + | /// Degree → 천분율 (단위 포함) | ||
| + | /// (-122.333333 → 122:20.660 W) | ||
| + | /// </ | ||
| + | /// <param name=" | ||
| + | /// < | ||
| + | public static string Degree2Permil(double degree) | ||
| + | { | ||
| + | string unit = string.Empty; | ||
| + | string fmt = string.Empty; | ||
| + | |||
| + | double absVal = Math.Abs(degree); | ||
| + | |||
| + | if ((int)absVal <= 90) | ||
| + | { | ||
| + | fmt = " | ||
| + | unit = degree >= 0 ? " N" : " S"; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | fmt = " | ||
| + | unit = degree >= 0 ? " E" : " W"; | ||
| + | } | ||
| + | |||
| + | | ||
| + | return ((int)absVal).ToString(fmt) + ":" | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | /// < | ||
| + | /// 도분초 (단위포함) → Degree | ||
| + | /// (122:20:00 W → -122.333333) | ||
| + | /// </ | ||
| + | /// <param name=" | ||
| + | /// < | ||
| + | public static double DMS2Degree(string dms) | ||
| + | { | ||
| + | int unitIndex = dms.LastIndexOf(' | ||
| + | string unit = dms.Substring(unitIndex + 1, 1).Trim(); | ||
| + | |||
| + | |||
| + | string[] dmsValue = dms.Substring(0, | ||
| + | |||
| + | double result = double.Parse(dmsValue[0]) + (double)(double.Parse(dmsValue[1]) / (double)60) + (double)(double.Parse(dmsValue[2]) / (double)3600); | ||
| + | |||
| + | if (unit.ToUpper().Equals(" | ||
| + | result = -result; | ||
| + | |||
| + | return result; | ||
| + | } | ||
| + | |||
| + | /// < | ||
| + | /// 천분율 (단위포함) → Degree | ||
| + | /// (122:20.660 W → -122.333333) | ||
| + | /// </ | ||
| + | /// <param name=" | ||
| + | /// < | ||
| + | public static double Permil2Degree(string permil) | ||
| + | { | ||
| + | int unitIndex = permil.LastIndexOf(' | ||
| + | string unit = permil.Substring(unitIndex + 1, 1).Trim(); | ||
| + | |||
| + | |||
| + | string permilValue = permil.Substring(0, | ||
| + | |||
| + | int colonIndex = permilValue.IndexOf(':' | ||
| + | |||
| + | int deg = int.Parse(permilValue.Substring(0, | ||
| + | double min = double.Parse(permilValue.Substring(colonIndex + 1, permilValue.Length - (colonIndex + 1))); | ||
| + | |||
| + | |||
| + | double result = (double)deg + min / 60; | ||
| + | |||
| + | if (unit.ToUpper().Equals(" | ||
| + | result = -result; | ||
| + | |||
| + | return result; | ||
| } | } | ||
| </ | </ | ||
| Line 470: | Line 592: | ||
| - | === 중심점으로부터 | + | === 각도, |
| <sxh csharp> | <sxh csharp> | ||
| - | | + | |
| { | { | ||
| - | double radian = 3.1415926535897932 / (180 / degree); | + | double radian = Degree2Radian(degree); |
| + | return Tuple.Create(radius * Math.Cos(radian), | ||
| + | } | ||
| - | Point p = new Point(); | + | public Tuple< |
| - | | + | { |
| - | | + | |
| + | | ||
| + | return Tuple.Create(degree, | ||
| + | } | ||
| - | | + | public double Degree2Radian(double degree) |
| + | { | ||
| + | | ||
| } | } | ||
| </ | </ | ||
| Line 517: | Line 646: | ||
| == 정렬 알고리즘(Sort Algorithm) == | == 정렬 알고리즘(Sort Algorithm) == | ||
| === 합병 정렬(Merge Sort) === | === 합병 정렬(Merge Sort) === | ||
| - | [[Merge Sort | 합병 정렬(Merge Sort)]] | + | [[algorithm:Merge Sort | 합병 정렬(Merge Sort)]] |
| + | |||
| + | |||
| + | = Language & FrameWork = | ||
| + | |||
| + | == Java == | ||
| + | |||
| + | === 양력/ | ||
| + | |||
| + | {{: | ||
| + | |||
| + | * http:// | ||
| + | * https:// | ||
| - | === 중복되지 않는 난수 생성 === | ||
| <sxh java> | <sxh java> | ||
| - | //language : Java | + | import org.joda.time.DateTime; |
| - | //1~5 중복되지 않는 난수 발생 | + | |
| - | + | ||
| - | int[] arr = new int[5]; | + | |
| - | Random random = new Random(); | + | |
| - | + | ||
| - | for(int i=0 ; i<arr.length ; i++) { | + | |
| - | arr[i] = random.nextInt(arr.length)+1; | + | |
| - | + | ||
| - | for(int j=0 ; j<i ; j++) { | + | |
| - | if(j!=i && arr[j] == arr[i]) | + | |
| - | i--; | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| + | import com.ibm.icu.util.ChineseCalendar; | ||
| + | public class LunarDateTime { | ||
| + | |||
| + | private static int yearCorrectionValue = 2637; | ||
| - | = Java & C# = | + | private LunarDateTime() { |
| + | } | ||
| + | |||
| + | /** | ||
| + | * | ||
| + | * @return 오늘 음력날짜 | ||
| + | */ | ||
| + | public static DateTime now() { | ||
| + | ChineseCalendar cc = new ChineseCalendar(); | ||
| + | |||
| + | int year = cc.get(ChineseCalendar.EXTENDED_YEAR) - yearCorrectionValue; | ||
| + | int monthOfYear = cc.get(ChineseCalendar.MONTH) + 1; | ||
| + | int dayOfMonth = cc.get(ChineseCalendar.DAY_OF_MONTH); | ||
| + | |||
| + | int[] hms = getNowHMS(); | ||
| + | return new DateTime(year, | ||
| + | } | ||
| + | |||
| + | public static DateTime solarToLunar(int solarYear, int solarMonthOfYear, | ||
| + | |||
| + | int[] hms = getNowHMS(); | ||
| + | DateTime dt = new DateTime(solarYear, | ||
| - | == C# - Serialization을 이용한 Deep Copy == | + | ChineseCalendar cc = new ChineseCalendar(); |
| - | <sxh csharp> | + | cc.setTimeInMillis(dt.getMillis()); |
| - | public static | + | |
| - | | + | int year = cc.get(ChineseCalendar.EXTENDED_YEAR) |
| - | using (var ms = new MemoryStream()) | + | int monthOfYear |
| - | { | + | int dayOfMonth |
| - | var formatter = new BinaryFormatter(); | + | |
| - | | + | return new DateTime(year, |
| - | ms.Position = 0; | + | } |
| + | |||
| + | public static | ||
| + | ChineseCalendar cc = new ChineseCalendar(); | ||
| + | cc.set(ChineseCalendar.EXTENDED_YEAR, | ||
| + | cc.set(ChineseCalendar.MONTH, lunarMonthOfYear - 1); | ||
| + | cc.set(ChineseCalendar.DAY_OF_MONTH, | ||
| - | | + | int[] hms = getNowHMS(); |
| - | } | + | cc.set(ChineseCalendar.HOUR_OF_DAY, |
| - | } | + | cc.set(ChineseCalendar.MINUTE, |
| + | cc.set(ChineseCalendar.SECOND, | ||
| + | |||
| + | | ||
| + | } | ||
| + | |||
| + | private static int[] getNowHMS() { | ||
| + | DateTime now = DateTime.now(); | ||
| + | return new int[] {now.getHourOfDay(), | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| - | == C# - Lambda 이용하여 오름차순 정렬 == | ||
| - | <sxh csharp> | ||
| - | List< | ||
| - | points.Add(new Point(11.0, 13.0)); | ||
| - | //... | ||
| - | points.Sort((Point point1, Point point2) => point1.Y.CompareTo(point2.Y)); | ||
| - | </ | ||
| - | == 포인터와 구조체 기초 개념 예제 == | + | |
| + | |||
| + | |||
| + | == C == | ||
| + | |||
| + | === 포인터와 구조체 기초 개념 예제 | ||
| <sxh c ; collapse: | <sxh c ; collapse: | ||
| #include < | #include < | ||
| Line 683: | Line 848: | ||
| </ | </ | ||
| - | = C = | + | === 동적 배열 할당 |
| - | + | ||
| - | == 동적 배열 할당 == | + | |
| <sxh c> | <sxh c> | ||
| //1차원 | //1차원 | ||
| Line 702: | Line 865: | ||
| - | = Android = | + | == Android |
| - | == Parcelable == | + | === Parcelable |
| * Intent를 이용하여 **Reference Type**의 데이터를 넘길 때 사용. | * Intent를 이용하여 **Reference Type**의 데이터를 넘길 때 사용. | ||
| http:// | http:// | ||
| - | == Uri에서 | + | === Image 압축 및 실제 경로 |
| - | * SD Card에 저장된 그림 파일을 ImageView에 붙일 필요가 있을 때 사용. | + | |
| <sxh java> | <sxh java> | ||
| - | private String getRealImagePath (Uri uri) { | + | public class ImageUtil { |
| - | Cursor cursor = getContentResolver().query(uri, | + | |
| - | cursor.moveToFirst(); | + | |
| - | int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); | + | private static final int MAX_IMAGE_LENGTH = 720 * 1280; |
| - | return cursor.getString(index); | + | |
| + | /** | ||
| + | * Image Uri → Compressed byte array | ||
| + | * (반복문으로 인하여 별도의 Thread 필요) | ||
| + | * @param context | ||
| + | * @param uri | ||
| + | * @return | ||
| + | * @throws Exception | ||
| + | */ | ||
| + | public static byte[] getCompressedByteArray(Context context, Uri uri) throws Exception { | ||
| + | BitmapFactory.Options options = new BitmapFactory.Options(); | ||
| + | |||
| + | options.inSampleSize = calculateInSampleSize(options, | ||
| + | options.inJustDecodeBounds = false; | ||
| + | options.inPreferredConfig= Bitmap.Config.ARGB_8888; | ||
| + | |||
| + | BufferedInputStream bin = new BufferedInputStream(context.getContentResolver().openInputStream(uri)); | ||
| + | Bitmap bitmap = BitmapFactory.decodeStream(bin, | ||
| + | |||
| + | // resize (지정한 크기보다 작은 경우만) | ||
| + | if(bitmap.getWidth() * bitmap.getHeight() > MAX_IMAGE_SIZE * 2) | ||
| + | bitmap = resizeBitmap(bitmap, | ||
| + | |||
| + | int compressQuality = 100; | ||
| + | int streamLength; | ||
| + | |||
| + | byte[] bitmapdata; | ||
| + | |||
| + | | ||
| + | String extension = realPath.substring(realPath.lastIndexOf(' | ||
| + | Bitmap.CompressFormat format; | ||
| + | switch (extension.toLowerCase()) { | ||
| + | case " | ||
| + | case " | ||
| + | format = Bitmap.CompressFormat.JPEG; | ||
| + | break; | ||
| + | |||
| + | case " | ||
| + | case " | ||
| + | format = Bitmap.CompressFormat.PNG; | ||
| + | break; | ||
| + | |||
| + | case " | ||
| + | format = Bitmap.CompressFormat.WEBP; | ||
| + | break; | ||
| + | |||
| + | default: | ||
| + | throw new Exception(" | ||
| + | } | ||
| + | |||
| + | do { | ||
| + | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| + | bitmap.compress(format, | ||
| + | bitmapdata = baos.toByteArray(); | ||
| + | streamLength = bitmapdata.length; | ||
| + | compressQuality -= 5; | ||
| + | } while (streamLength >= MAX_IMAGE_LENGTH); | ||
| + | |||
| + | Log.d(" | ||
| + | Log.d(" | ||
| + | |||
| + | return bitmapdata; | ||
| + | } | ||
| + | |||
| + | private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { | ||
| + | final int height = options.outHeight; | ||
| + | final int width = options.outWidth; | ||
| + | int inSampleSize = 1; | ||
| + | |||
| + | if (height > reqHeight || width > reqWidth) { | ||
| + | |||
| + | final int halfHeight = height / 2; | ||
| + | final int halfWidth = width / 2; | ||
| + | |||
| + | // Calculate the largest inSampleSize value that is a power of 2 and keeps both | ||
| + | // height and width larger than the requested height and width. | ||
| + | while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { | ||
| + | inSampleSize *= 2; | ||
| + | } | ||
| + | } | ||
| + | Log.d(" | ||
| + | return inSampleSize; | ||
| + | } | ||
| + | |||
| + | private static Bitmap resizeBitmap(Bitmap bitmap, int maxSize) { | ||
| + | float ratio = Math.min((float) maxSize / bitmap.getWidth(), | ||
| + | int width = Math.round(ratio * bitmap.getWidth()); | ||
| + | int height = Math.round(ratio * bitmap.getHeight()); | ||
| + | return Bitmap.createScaledBitmap(bitmap, | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Uri를 이용해 실제 저장소 경로 얻기 | ||
| + | * @param context | ||
| + | * @param uri | ||
| + | * @return | ||
| + | */ | ||
| + | public static String getRealImagePath(Context context, | ||
| + | Cursor cursor = context.getContentResolver().query(uri, | ||
| + | cursor.moveToFirst(); | ||
| + | int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); | ||
| + | return cursor.getString(index); | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * [0] : fileName | ||
| + | * [1] : extension | ||
| + | * @param filePath | ||
| + | * @return | ||
| + | */ | ||
| + | public static String[] getFileNameAndExtension(String filePath) { | ||
| + | return new String[] {filePath.substring(filePath.lastIndexOf(File.pathSeparator) + 1), filePath.substring(filePath.lastIndexOf(' | ||
| + | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | = Intellij - Git Bash 연동 = | ||
| + | |||
| + | Tools -> Terminal | ||
| + | |||
| + | <sxh bash> | ||
| + | " | ||
| </ | </ | ||