parent
4567c66e9d
commit
7e3899bc2e
@ -0,0 +1,51 @@ |
|||||||
|
package com.flurgle.camerakit.utils; |
||||||
|
|
||||||
|
import android.graphics.Bitmap; |
||||||
|
import android.graphics.BitmapFactory; |
||||||
|
import android.graphics.Rect; |
||||||
|
import android.graphics.YuvImage; |
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream; |
||||||
|
|
||||||
|
public class CenterCrop { |
||||||
|
|
||||||
|
private byte[] croppedJpeg; |
||||||
|
|
||||||
|
public CenterCrop(YuvImage yuv, AspectRatio targetRatio) { |
||||||
|
Rect crop = getCrop(yuv.getWidth(), yuv.getHeight(), targetRatio); |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||||
|
yuv.compressToJpeg(crop, 100, out); |
||||||
|
this.croppedJpeg = out.toByteArray(); |
||||||
|
} |
||||||
|
|
||||||
|
public CenterCrop(byte[] jpeg, AspectRatio targetRatio) { |
||||||
|
Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length); |
||||||
|
Rect crop = getCrop(bitmap.getWidth(), bitmap.getHeight(), targetRatio); |
||||||
|
bitmap = Bitmap.createBitmap(bitmap, crop.left, crop.top, crop.width(), crop.height()); |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||||
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); |
||||||
|
this.croppedJpeg = out.toByteArray(); |
||||||
|
} |
||||||
|
|
||||||
|
public byte[] getJpeg() { |
||||||
|
return croppedJpeg; |
||||||
|
} |
||||||
|
|
||||||
|
private static Rect getCrop(int currentWidth, int currentHeight, AspectRatio targetRatio) { |
||||||
|
AspectRatio currentRatio = AspectRatio.of(currentWidth, currentHeight); |
||||||
|
|
||||||
|
Rect crop; |
||||||
|
if (currentRatio.toFloat() > targetRatio.toFloat()) { |
||||||
|
int width = (int) (currentHeight * targetRatio.toFloat()); |
||||||
|
int widthOffset = (currentWidth - width) / 2; |
||||||
|
crop = new Rect(widthOffset, 0, currentWidth - widthOffset, currentHeight); |
||||||
|
} else { |
||||||
|
int height = (int) (currentWidth * targetRatio.inverse().toFloat()); |
||||||
|
int heightOffset = (currentHeight - height) / 2; |
||||||
|
crop = new Rect(0, heightOffset, currentWidth, currentHeight - heightOffset); |
||||||
|
} |
||||||
|
|
||||||
|
return crop; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,37 @@ |
|||||||
|
package com.flurgle.camerakit.demo; |
||||||
|
|
||||||
|
import android.graphics.Bitmap; |
||||||
|
import android.support.annotation.Nullable; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.lang.ref.WeakReference; |
||||||
|
|
||||||
|
public class MediaHolder { |
||||||
|
|
||||||
|
private static WeakReference<File> video; |
||||||
|
private static WeakReference<Bitmap> image; |
||||||
|
|
||||||
|
public static void setVideo(@Nullable File video) { |
||||||
|
MediaHolder.video = video != null ? new WeakReference<>(video) : null; |
||||||
|
} |
||||||
|
|
||||||
|
@Nullable |
||||||
|
public static File getVideo() { |
||||||
|
return video != null ? video.get() : null; |
||||||
|
} |
||||||
|
|
||||||
|
public static void setImage(@Nullable Bitmap image) { |
||||||
|
MediaHolder.image = image != null ? new WeakReference<>(image) : null; |
||||||
|
} |
||||||
|
|
||||||
|
@Nullable |
||||||
|
public static Bitmap getImage() { |
||||||
|
return image != null ? image.get() : null; |
||||||
|
} |
||||||
|
|
||||||
|
public static void dispose() { |
||||||
|
setVideo(null); |
||||||
|
setImage(null); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,3 +1,3 @@ |
|||||||
<resources> |
<resources> |
||||||
<string name="app_name">CameraKit Demo</string> |
<string name="app_name">CameraKit</string> |
||||||
</resources> |
</resources> |
||||||
|
Loading…
Reference in new issue