parent
4481c7ad45
commit
fa9fba1f90
@ -0,0 +1,28 @@ |
|||||||
|
package com.flurgle.camerakit; |
||||||
|
|
||||||
|
|
||||||
|
import android.support.media.ExifInterface; |
||||||
|
|
||||||
|
class ExifUtils { |
||||||
|
|
||||||
|
public static int getOrientationTag(int rotation, boolean flip) { |
||||||
|
switch (rotation) { |
||||||
|
case 90: |
||||||
|
return flip ? ExifInterface.ORIENTATION_TRANSPOSE : |
||||||
|
ExifInterface.ORIENTATION_ROTATE_90; |
||||||
|
|
||||||
|
case 180: |
||||||
|
return flip ? ExifInterface.ORIENTATION_FLIP_VERTICAL : |
||||||
|
ExifInterface.ORIENTATION_ROTATE_180; |
||||||
|
|
||||||
|
case 270: |
||||||
|
return flip ? ExifInterface.ORIENTATION_TRANSVERSE : |
||||||
|
ExifInterface.ORIENTATION_ROTATE_270; |
||||||
|
|
||||||
|
case 0: |
||||||
|
default: |
||||||
|
return flip ? ExifInterface.ORIENTATION_FLIP_HORIZONTAL : |
||||||
|
ExifInterface.ORIENTATION_NORMAL; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,67 +0,0 @@ |
|||||||
package com.flurgle.camerakit; |
|
||||||
|
|
||||||
import android.graphics.Bitmap; |
|
||||||
import android.graphics.BitmapFactory; |
|
||||||
import android.graphics.BitmapRegionDecoder; |
|
||||||
import android.graphics.Rect; |
|
||||||
import android.graphics.YuvImage; |
|
||||||
import android.util.Log; |
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream; |
|
||||||
import java.io.IOException; |
|
||||||
|
|
||||||
public class CenterCrop { |
|
||||||
|
|
||||||
private byte[] croppedJpeg; |
|
||||||
|
|
||||||
public CenterCrop(YuvImage yuv, AspectRatio targetRatio, int jpegCompression) { |
|
||||||
Rect crop = getCrop(yuv.getWidth(), yuv.getHeight(), targetRatio); |
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
||||||
yuv.compressToJpeg(crop, jpegCompression, out); |
|
||||||
this.croppedJpeg = out.toByteArray(); |
|
||||||
} |
|
||||||
|
|
||||||
public CenterCrop(byte[] jpeg, AspectRatio targetRatio, int jpegCompression) { |
|
||||||
BitmapFactory.Options options = new BitmapFactory.Options(); |
|
||||||
options.inJustDecodeBounds = true; |
|
||||||
BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length, options); |
|
||||||
|
|
||||||
Rect crop = getCrop(options.outWidth, options.outHeight, targetRatio); |
|
||||||
try { |
|
||||||
Bitmap bitmap = BitmapRegionDecoder.newInstance( |
|
||||||
jpeg, |
|
||||||
0, |
|
||||||
jpeg.length, |
|
||||||
true |
|
||||||
).decodeRegion(crop, null); |
|
||||||
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, jpegCompression, out); |
|
||||||
this.croppedJpeg = out.toByteArray(); |
|
||||||
} catch (IOException e) { |
|
||||||
Log.e("CameraKit", e.toString()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
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; |
|
||||||
} |
|
||||||
|
|
||||||
public byte[] getJpeg() { |
|
||||||
return croppedJpeg; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,39 +0,0 @@ |
|||||||
package com.flurgle.camerakit; |
|
||||||
|
|
||||||
import java.util.HashSet; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Set; |
|
||||||
import java.util.TreeSet; |
|
||||||
|
|
||||||
public class CommonAspectRatioFilter { |
|
||||||
|
|
||||||
private List<Size> mPreviewSizes; |
|
||||||
private List<Size> mCaptureSizes; |
|
||||||
|
|
||||||
public CommonAspectRatioFilter(List<Size> previewSizes, List<Size> captureSizes) { |
|
||||||
this.mPreviewSizes = previewSizes; |
|
||||||
this.mCaptureSizes = captureSizes; |
|
||||||
} |
|
||||||
|
|
||||||
public TreeSet<AspectRatio> filter() { |
|
||||||
Set<AspectRatio> previewAspectRatios = new HashSet<>(); |
|
||||||
for (Size size : mPreviewSizes) { |
|
||||||
previewAspectRatios.add(AspectRatio.of(size.getWidth(), size.getHeight())); |
|
||||||
} |
|
||||||
|
|
||||||
Set<AspectRatio> captureAspectRatios = new HashSet<>(); |
|
||||||
for (Size size : mCaptureSizes) { |
|
||||||
captureAspectRatios.add(AspectRatio.of(size.getWidth(), size.getHeight())); |
|
||||||
} |
|
||||||
|
|
||||||
TreeSet<AspectRatio> output = new TreeSet<>(); |
|
||||||
for (AspectRatio aspectRatio : previewAspectRatios) { |
|
||||||
if (captureAspectRatios.contains(aspectRatio)) { |
|
||||||
output.add(aspectRatio); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return output; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,53 @@ |
|||||||
|
package com.flurgle.camerakit; |
||||||
|
|
||||||
|
import android.graphics.Bitmap; |
||||||
|
import android.graphics.BitmapRegionDecoder; |
||||||
|
import android.graphics.Rect; |
||||||
|
import android.graphics.YuvImage; |
||||||
|
import android.util.Log; |
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream; |
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
public class CropHelper { |
||||||
|
|
||||||
|
|
||||||
|
// TODO test this. How is YuvImage? Does it come already well rotated?
|
||||||
|
public static byte[] cropToJpeg(YuvImage yuv, AspectRatio targetRatio, int jpegCompression) { |
||||||
|
Rect crop = computeCrop(yuv.getWidth(), yuv.getHeight(), targetRatio); |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||||
|
yuv.compressToJpeg(crop, jpegCompression, out); |
||||||
|
return out.toByteArray(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// This reads a rotated Bitmap thanks to CameraUtils. Then crops and returns a byte array.
|
||||||
|
// In doing so, EXIF data is deleted.
|
||||||
|
public static byte[] cropToJpeg(byte[] jpeg, AspectRatio targetRatio, int jpegCompression) { |
||||||
|
Bitmap image = CameraUtils.decodeBitmap(jpeg); |
||||||
|
Log.e("CropHelper", "decoded image has width="+image.getWidth()+", height="+image.getHeight()); |
||||||
|
Rect cropRect = computeCrop(image.getWidth(), image.getHeight(), targetRatio); |
||||||
|
Bitmap crop = Bitmap.createBitmap(image, cropRect.left, cropRect.top, cropRect.width(), cropRect.height()); |
||||||
|
image.recycle(); |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||||
|
crop.compress(Bitmap.CompressFormat.JPEG, jpegCompression, out); |
||||||
|
return out.toByteArray(); |
||||||
|
} |
||||||
|
|
||||||
|
private static Rect computeCrop(int currentWidth, int currentHeight, AspectRatio targetRatio) { |
||||||
|
AspectRatio currentRatio = AspectRatio.of(currentWidth, currentHeight); |
||||||
|
int x, y, width, height; |
||||||
|
if (currentRatio.toFloat() > targetRatio.toFloat()) { |
||||||
|
height = currentHeight; |
||||||
|
width = (int) (height * targetRatio.toFloat()); |
||||||
|
y = 0; |
||||||
|
x = (currentWidth - width) / 2; |
||||||
|
} else { |
||||||
|
width = currentWidth; |
||||||
|
height = (int) (width * targetRatio.inverse().toFloat()); |
||||||
|
y = (currentHeight - height) / 2; |
||||||
|
x = 0; |
||||||
|
} |
||||||
|
return new Rect(x, y, x+width, y+height); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue