This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programming_tips_and_tricks [2016/01/14 05:39] – [경위도 변환] ledyx | programming_tips_and_tricks [2021/02/07 03:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 646: | Line 646: | ||
| == 정렬 알고리즘(Sort Algorithm) == | == 정렬 알고리즘(Sort Algorithm) == | ||
| === 합병 정렬(Merge Sort) === | === 합병 정렬(Merge Sort) === | ||
| - | [[Merge Sort | 합병 정렬(Merge Sort)]] | + | [[algorithm:Merge Sort | 합병 정렬(Merge Sort)]] |
| - | === 중복되지 않는 난수 생성 === | ||
| - | <sxh java> | ||
| - | //language : Java | ||
| - | //1~5 중복되지 않는 난수 발생 | ||
| - | |||
| - | int[] arr = new int[5]; | ||
| - | Random random = new Random(); | ||
| - | |||
| - | for(int i=0 ; i< | ||
| - | arr[i] = random.nextInt(arr.length)+1; | ||
| - | |||
| - | for(int j=0 ; j<i ; j++) { | ||
| - | if(j!=i && arr[j] == arr[i]) | ||
| - | i--; | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| + | = Language & FrameWork = | ||
| + | == Java == | ||
| - | = Java & C# = | + | === 양력/ |
| + | |||
| + | {{: | ||
| + | |||
| + | * http:// | ||
| + | * https:// | ||
| - | == Serialization을 이용한 Deep Copy == | ||
| <sxh java> | <sxh java> | ||
| - | /* Java */ | + | import org.joda.time.DateTime; |
| - | @SuppressWarnings(" | + | |
| - | public static <T> T deepClone(T obj) { | + | |
| - | try { | + | |
| - | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | + | |
| - | ObjectOutputStream oos = new ObjectOutputStream(baos); | + | |
| - | oos.writeObject(obj); | + | |
| - | ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); | + | import com.ibm.icu.util.ChineseCalendar; |
| - | ObjectInputStream ois = new ObjectInputStream(bais); | + | |
| - | return (T) ois.readObject(); | + | public class LunarDateTime |
| - | } catch (IOException e) { | + | |
| - | return null; | + | private static int yearCorrectionValue = 2637; |
| - | } catch (ClassNotFoundException e) { | + | |
| - | return null; | + | private LunarDateTime() { |
| } | } | ||
| - | } | + | |
| - | </sxh> | + | /** |
| - | https://github.com/ | + | * |
| + | * @return 오늘 음력날짜 | ||
| + | */ | ||
| + | public static DateTime now() { | ||
| + | ChineseCalendar cc = new ChineseCalendar(); | ||
| + | |||
| + | int year = cc.get(ChineseCalendar.EXTENDED_YEAR) | ||
| + | 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, | ||
| - | <sxh csharp> | + | ChineseCalendar cc = new ChineseCalendar(); |
| - | /* C# */ | + | cc.setTimeInMillis(dt.getMillis()); |
| - | public static T DeepClone< | + | |
| - | { | + | int year = cc.get(ChineseCalendar.EXTENDED_YEAR) - yearCorrectionValue; |
| - | | + | int monthOfYear |
| - | { | + | int dayOfMonth = cc.get(ChineseCalendar.DAY_OF_MONTH); |
| - | var formatter | + | |
| - | | + | return new DateTime(year, |
| - | ms.Position = 0; | + | } |
| + | |||
| + | public static DateTime lunar2Solar(int lunarYear, int lunarMonthOfYear, | ||
| + | ChineseCalendar cc = new ChineseCalendar(); | ||
| + | cc.set(ChineseCalendar.EXTENDED_YEAR, lunarYear + yearCorrectionValue); | ||
| + | cc.set(ChineseCalendar.MONTH, | ||
| + | cc.set(ChineseCalendar.DAY_OF_MONTH, | ||
| - | return | + | int[] hms = getNowHMS(); |
| - | } | + | cc.set(ChineseCalendar.HOUR_OF_DAY, |
| + | | ||
| + | cc.set(ChineseCalendar.SECOND, | ||
| + | |||
| + | return new DateTime(cc.getTimeInMillis()); | ||
| + | } | ||
| + | |||
| + | private static int[] getNowHMS() { | ||
| + | DateTime now = DateTime.now(); | ||
| + | return new int[] {now.getHourOfDay(), | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| - | https:// | ||
| - | == 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 836: | Line 848: | ||
| </ | </ | ||
| - | = C = | + | === 동적 배열 할당 |
| - | + | ||
| - | == 동적 배열 할당 == | + | |
| <sxh c> | <sxh c> | ||
| //1차원 | //1차원 | ||
| Line 855: | 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> | ||
| + | " | ||
| </ | </ | ||