Both sides previous revisionPrevious revisionNext revision | Previous revision |
programming_tips_and_tricks [2017/05/13 19:51] – [양력/음력 날짜 계산] ledyx | programming_tips_and_tricks [2021/02/07 03:15] (current) – external edit 127.0.0.1 |
---|
=== 합병 정렬(Merge Sort) === | === 합병 정렬(Merge Sort) === |
[[algorithm:Merge Sort | 합병 정렬(Merge Sort)]] | [[algorithm:Merge Sort | 합병 정렬(Merge Sort)]] |
| |
| |
= Design Pattern = | |
| |
== Serialization을 이용한 Deep Copy == | |
<sxh java> | |
/* Java */ | |
@SuppressWarnings("unchecked") | |
public static <T> T deepCopy(T obj) { | |
try { | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ObjectOutputStream oos = new ObjectOutputStream(baos); | |
oos.writeObject(obj); | |
| |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); | |
ObjectInputStream ois = new ObjectInputStream(bais); | |
return (T) ois.readObject(); | |
} catch (IOException e) { | |
return null; | |
} catch (ClassNotFoundException e) { | |
return null; | |
} | |
} | |
</sxh> | |
https://github.com/xeyez/CSharp-PrototypePattern | |
| |
<sxh csharp> | |
/* C# */ | |
public static T DeepCopy<T>(T obj) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
var formatter = new BinaryFormatter(); | |
formatter.Serialize(ms, obj); | |
ms.Position = 0; | |
| |
return (T)formatter.Deserialize(ms); | |
} | |
} | |
</sxh> | |
https://github.com/xeyez/Java-PrototypePattern | |
| |
| |
| |
| |
http://arsviator.tistory.com/169 | http://arsviator.tistory.com/169 |
| |
=== Uri에서 실제 경로를 받아오는 Method === | === Image 압축 및 실제 경로 얻기 === |
* SD Card에 저장된 그림 파일을 ImageView에 붙일 필요가 있을 때 사용. | |
<sxh java> | <sxh java> |
private String getRealImagePath (Uri uri) { | public class ImageUtil { |
Cursor cursor = getContentResolver().query(uri, null, null, null, null); | |
cursor.moveToFirst(); | private static final int MAX_IMAGE_SIZE = 1280; |
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, 800, 800); |
| options.inJustDecodeBounds = false; |
| options.inPreferredConfig= Bitmap.Config.ARGB_8888; |
| |
| BufferedInputStream bin = new BufferedInputStream(context.getContentResolver().openInputStream(uri)); |
| Bitmap bitmap = BitmapFactory.decodeStream(bin, null, options); |
| |
| // resize (지정한 크기보다 작은 경우만) |
| if(bitmap.getWidth() * bitmap.getHeight() > MAX_IMAGE_SIZE * 2) |
| bitmap = resizeBitmap(bitmap, MAX_IMAGE_SIZE); |
| |
| int compressQuality = 100; |
| int streamLength; |
| |
| byte[] bitmapdata; |
| |
| String realPath = getRealImagePath(context, uri); |
| String extension = realPath.substring(realPath.lastIndexOf('.') + 1); |
| Bitmap.CompressFormat format; |
| switch (extension.toLowerCase()) { |
| case "jpg" : |
| case "jpeg" : |
| format = Bitmap.CompressFormat.JPEG; |
| break; |
| |
| case "png" : |
| case "gif" : |
| format = Bitmap.CompressFormat.PNG; |
| break; |
| |
| case "webp" : |
| format = Bitmap.CompressFormat.WEBP; |
| break; |
| |
| default: |
| throw new Exception("Unsupported format!"); |
| } |
| |
| do { |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| bitmap.compress(format, compressQuality, baos); |
| bitmapdata = baos.toByteArray(); |
| streamLength = bitmapdata.length; |
| compressQuality -= 5; |
| } while (streamLength >= MAX_IMAGE_LENGTH); |
| |
| Log.d("compressBitmap", "Quality: " + compressQuality); |
| Log.d("compressBitmap", "Size: " + streamLength/1024+" kb"); |
| |
| 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("inSampleSize", String.valueOf(inSampleSize)); |
| return inSampleSize; |
| } |
| |
| private static Bitmap resizeBitmap(Bitmap bitmap, int maxSize) { |
| float ratio = Math.min((float) maxSize / bitmap.getWidth(), (float) maxSize / bitmap.getHeight()); |
| int width = Math.round(ratio * bitmap.getWidth()); |
| int height = Math.round(ratio * bitmap.getHeight()); |
| return Bitmap.createScaledBitmap(bitmap, width, height, false); |
| } |
| |
| /** |
| * Uri를 이용해 실제 저장소 경로 얻기 |
| * @param context |
| * @param uri |
| * @return |
| */ |
| public static String getRealImagePath(Context context, Uri uri) { |
| Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); |
| 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('.') + 1)}; |
| } |
} | } |
| </sxh> |
| |
| |
| = Intellij - Git Bash 연동 = |
| |
| Tools -> Terminal |
| |
| <sxh bash> |
| "C:\Program Files\Git\bin\sh.exe" -login -i |
</sxh> | </sxh> |