diff --git a/library/src/main/java/com/google/android/cameraview/CameraView.java b/library/src/main/java/com/google/android/cameraview/CameraView.java index ede5dad8..2349a3ff 100644 --- a/library/src/main/java/com/google/android/cameraview/CameraView.java +++ b/library/src/main/java/com/google/android/cameraview/CameraView.java @@ -30,7 +30,6 @@ import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; -import android.graphics.RectF; import android.os.Build; import android.os.Parcel; import android.os.Parcelable; @@ -59,6 +58,14 @@ public class CameraView extends FrameLayout { /** The camera device faces the same direction as the device's screen. */ public static final int FACING_FRONT = Constants.FACING_FRONT; + public static final String SELFIE = "SELFIE"; + public static final String KTP = "KTP"; + public static final String SIM = "SIM"; + public static final String NPWP = "NPWP"; + public static final String PASSPORT = "PASSPORT"; + public static final String SELFIE_ID = "SELFIE_ID"; + + private final int TITLE_MARGIN_BOTTOM = 20; private Bitmap bitmap; /** Direction the camera faces relative to device screen. */ @@ -97,6 +104,8 @@ public class CameraView extends FrameLayout { private int deviceWidth; private final DisplayOrientationDetector mDisplayOrientationDetector; + private String kycImageType; + private String title; public CameraView(Context context) { this(context, null); @@ -129,16 +138,17 @@ public CameraView(Context context, AttributeSet attrs, int defStyleAttr) { R.style.Widget_CameraView); mAdjustViewBounds = a.getBoolean(R.styleable.CameraView_android_adjustViewBounds, false); setFacing(a.getInt(R.styleable.CameraView_facing, FACING_BACK)); - getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - getViewTreeObserver().removeOnGlobalLayoutListener(this); - DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); - deviceWidth = displayMetrics.widthPixels; - deviceHeight = displayMetrics.heightPixels; - setAspectRatio(AspectRatio.of(deviceHeight, deviceWidth)); - } - }); + getViewTreeObserver().addOnGlobalLayoutListener( + new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + getViewTreeObserver().removeOnGlobalLayoutListener(this); + DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); + deviceWidth = displayMetrics.widthPixels; + deviceHeight = displayMetrics.heightPixels; + setAspectRatio(AspectRatio.of(deviceHeight, deviceWidth)); + } + }); setAutoFocus(a.getBoolean(R.styleable.CameraView_autoFocus, true)); setFlash(a.getInt(R.styleable.CameraView_flash, Constants.FLASH_AUTO)); a.recycle(); @@ -151,6 +161,14 @@ public void onDisplayOrientationChanged(int displayOrientation) { }; } + public void setKycImageType(String kycImageType) { + this.kycImageType = kycImageType; + } + + public void setTitleText(String title) { + this.title = title; + } + @NonNull private PreviewImpl createPreviewImpl(Context context) { PreviewImpl preview; @@ -460,45 +478,91 @@ public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { protected void createWindowFrame() { bitmap = Bitmap.createBitmap(deviceWidth, deviceHeight, Bitmap.Config.ARGB_8888); - Canvas osCanvas = new Canvas(bitmap); - Bitmap face = BitmapFactory.decodeResource(getResources(), - R.drawable.frame_face); - face = getResizedBitmap(face, dpToPx(250), dpToPx(400)); - Bitmap faceMask = BitmapFactory.decodeResource(getResources(), - R.drawable.frame_face_mask); - faceMask = getResizedBitmap(faceMask, dpToPx(250), dpToPx(400)); + Canvas canvas = new Canvas(bitmap); +// Bitmap face = BitmapFactory.decodeResource(getResources(), +// R.drawable.frame_face); +// face = getResizedBitmap(face, dpToPx(250), dpToPx(400)); +// faceMask = getResizedBitmap(faceMask, dpToPx(250), dpToPx(400)); Rect outerRectangle = new Rect(0, 0, deviceWidth, deviceHeight); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(getResources().getColor(android.R.color.black)); paint.setAlpha(200); - osCanvas.drawRect(outerRectangle, paint); + canvas.drawRect(outerRectangle, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR)); int centerX = deviceWidth / 2; int centerY = deviceHeight / 2; int radius = dpToPx(12); - int radiusx = dpToPx(250); - int radiusy = dpToPx(400); - -// osCanvas.drawPath( -// roundedRect(centerX - 400, centerY - 250, centerX + 400, centerY + 250, radius, -// radius, false), paint); -// osCanvas.drawBitmap(ktp, null, -// new Rect(centerX - 400, centerY - 250, centerX + 400, centerY + 250), null); -// osCanvas.drawPath( -// face(centerX - 250, centerY - 400, centerX + 250, centerY + 400, radiusx, -// radiusy), paint); - osCanvas.drawBitmap(faceMask, centerX - radiusx / 2, centerY - radiusy / 2, paint); - osCanvas.drawBitmap(face, centerX - radiusx / 2, centerY - radiusy / 2, null); -// osCanvas.drawBitmap(ktp, outerRectangle, + Paint textPaint = new Paint(); + textPaint.setStyle(Paint.Style.FILL); + textPaint.setColor(Color.WHITE); + textPaint.setTextAlign(Paint.Align.CENTER); + textPaint.setTextSize(spToPx(14)); + switch (kycImageType) { + case SELFIE: { +// Bitmap face = BitmapFactory.decodeResource(getResources(), +// R.drawable.frame_face); +// Bitmap faceMask = BitmapFactory.decodeResource(getResources(), +// R.drawable.frame_face_mask); +// int height = face.getHeight(); +// int width = face.getWidth(); +// canvas.drawBitmap(faceMask, centerX - radiusx / 2, centerY - radiusy / 2, +// paint); +// canvas.drawBitmap(face, centerX - radiusx / 2, centerY - radiusy / 2, null); + break; + } + case KTP: { + Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.frame_ktp); + drawRoundedRectBitmap(picture, centerX, centerY, canvas, radius, paint, textPaint); + break; + } + case SIM: { + Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.frame_sim); + drawRoundedRectBitmap(picture, centerX, centerY, canvas, radius, paint, textPaint); + break; + } + case NPWP: { + Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.frame_npwp); + drawRoundedRectBitmap(picture, centerX, centerY, canvas, radius, paint, textPaint); + break; + } + case PASSPORT: { + Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.frame_passport); + drawRoundedRectBitmap(picture, centerX, centerY, canvas, radius, paint, textPaint); + break; + } + } +// canvas.drawBitmap(ktp, outerRectangle, // new Rect(centerX - 250, centerY - 400, centerX + 250, centerY + 400), null); -// osCanvas.drawBitmap(ktp, null, +// canvas.drawBitmap(ktp, null, // new Rect(centerX - 250, centerY - 400, centerX + 250, centerY + 400), null); } - public static int dpToPx(int dp) { - return (int) (dp * Resources.getSystem().getDisplayMetrics().density); + private void drawRoundedRectBitmap(Bitmap picture, int centerX, int centerY, Canvas canvas, + int radius, Paint paint, Paint textPaint) { + int height = picture.getHeight(); + int width = picture.getWidth(); + canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint); + // draw masking + canvas.drawPath( + roundedRect(centerX - (width / 2), centerY - (height / 2), + centerX + (width / 2), centerY + (height / 2), radius, + radius, false), paint); + // draw the picture itself + canvas.drawBitmap(picture, null, + new Rect(centerX - (width / 2), centerY - (height / 2), + centerX + (width / 2), centerY + (height / 2)), null); + } + + private int dpToPx(int dp) { + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, + Resources.getSystem().getDisplayMetrics()); + } + + private int spToPx(int sp) { + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, + Resources.getSystem().getDisplayMetrics()); } public Path roundedRect(float left, float top, float right, float bottom, float rx, float ry, @@ -536,37 +600,6 @@ public Path roundedRect(float left, float top, float right, float bottom, float return path; } - public Path face(float left, float top, float right, float bottom, float rx, float ry) { - Path path = new Path(); - if (rx < 0) rx = 0; - if (ry < 0) ry = 0; - float width = right - left; - float height = bottom - top; - if (rx > width / 2) rx = width / 2; - if (ry > height / 2) ry = height / 2; - float widthMinusCorners = (width - (2 * rx)); - float heightMinusCorners = (height - (2 * ry)); - - float ry1 = ry; - float ry2 = ry; - path.moveTo(right, top + ry1); - path.rQuadTo(0, -ry1, -rx, -ry1);//top-right corner -// path.rLineTo(-widthMinusCorners, 0); - path.rQuadTo(-rx, 0, -rx, ry1); //top-left corner -// path.rLineTo(0, heightMinusCorners); - - - path.rQuadTo(0, ry2, rx, ry2);//bottom-left corner -// path.rLineTo(widthMinusCorners, 0); - path.rQuadTo(rx, 0, rx, -ry2); //bottom-right corner - -// path.rLineTo(0, -heightMinusCorners); - - path.close();//Given close, last lineto can be removed. - - return path; - } - @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); diff --git a/library/src/main/res/drawable-hdpi/frame_ktp.png b/library/src/main/res/drawable-hdpi/frame_ktp.png new file mode 100644 index 00000000..b44efb85 Binary files /dev/null and b/library/src/main/res/drawable-hdpi/frame_ktp.png differ diff --git a/library/src/main/res/drawable-hdpi/frame_npwp.png b/library/src/main/res/drawable-hdpi/frame_npwp.png new file mode 100644 index 00000000..6fa22b81 Binary files /dev/null and b/library/src/main/res/drawable-hdpi/frame_npwp.png differ diff --git a/library/src/main/res/drawable-hdpi/frame_passport.png b/library/src/main/res/drawable-hdpi/frame_passport.png new file mode 100644 index 00000000..f2ffd794 Binary files /dev/null and b/library/src/main/res/drawable-hdpi/frame_passport.png differ diff --git a/library/src/main/res/drawable-hdpi/frame_sim.png b/library/src/main/res/drawable-hdpi/frame_sim.png new file mode 100644 index 00000000..9b5758cd Binary files /dev/null and b/library/src/main/res/drawable-hdpi/frame_sim.png differ diff --git a/library/src/main/res/drawable-mdpi/frame_ktp.png b/library/src/main/res/drawable-mdpi/frame_ktp.png new file mode 100644 index 00000000..5e3de53f Binary files /dev/null and b/library/src/main/res/drawable-mdpi/frame_ktp.png differ diff --git a/library/src/main/res/drawable-mdpi/frame_npwp.png b/library/src/main/res/drawable-mdpi/frame_npwp.png new file mode 100644 index 00000000..8a534fe4 Binary files /dev/null and b/library/src/main/res/drawable-mdpi/frame_npwp.png differ diff --git a/library/src/main/res/drawable-mdpi/frame_passport.png b/library/src/main/res/drawable-mdpi/frame_passport.png new file mode 100644 index 00000000..c4e21795 Binary files /dev/null and b/library/src/main/res/drawable-mdpi/frame_passport.png differ diff --git a/library/src/main/res/drawable-mdpi/frame_sim.png b/library/src/main/res/drawable-mdpi/frame_sim.png new file mode 100644 index 00000000..a2b95608 Binary files /dev/null and b/library/src/main/res/drawable-mdpi/frame_sim.png differ diff --git a/library/src/main/res/drawable-xhdpi/frame_ktp.png b/library/src/main/res/drawable-xhdpi/frame_ktp.png new file mode 100644 index 00000000..c55b4eb2 Binary files /dev/null and b/library/src/main/res/drawable-xhdpi/frame_ktp.png differ diff --git a/library/src/main/res/drawable-xhdpi/frame_npwp.png b/library/src/main/res/drawable-xhdpi/frame_npwp.png new file mode 100644 index 00000000..997586b0 Binary files /dev/null and b/library/src/main/res/drawable-xhdpi/frame_npwp.png differ diff --git a/library/src/main/res/drawable-xhdpi/frame_passport.png b/library/src/main/res/drawable-xhdpi/frame_passport.png new file mode 100644 index 00000000..6d6bd6c9 Binary files /dev/null and b/library/src/main/res/drawable-xhdpi/frame_passport.png differ diff --git a/library/src/main/res/drawable-xhdpi/frame_sim.png b/library/src/main/res/drawable-xhdpi/frame_sim.png new file mode 100644 index 00000000..fc462703 Binary files /dev/null and b/library/src/main/res/drawable-xhdpi/frame_sim.png differ diff --git a/library/src/main/res/drawable/frame_ktp.png b/library/src/main/res/drawable-xxhdpi/frame_ktp.png similarity index 100% rename from library/src/main/res/drawable/frame_ktp.png rename to library/src/main/res/drawable-xxhdpi/frame_ktp.png diff --git a/library/src/main/res/drawable-xxhdpi/frame_npwp.png b/library/src/main/res/drawable-xxhdpi/frame_npwp.png new file mode 100644 index 00000000..e616d2bc Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/frame_npwp.png differ diff --git a/library/src/main/res/drawable-xxhdpi/frame_passport.png b/library/src/main/res/drawable-xxhdpi/frame_passport.png new file mode 100644 index 00000000..69d37798 Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/frame_passport.png differ diff --git a/library/src/main/res/drawable-xxhdpi/frame_sim.png b/library/src/main/res/drawable-xxhdpi/frame_sim.png new file mode 100644 index 00000000..0c66454b Binary files /dev/null and b/library/src/main/res/drawable-xxhdpi/frame_sim.png differ diff --git a/library/src/main/res/drawable-xxxhdpi/frame_ktp.png b/library/src/main/res/drawable-xxxhdpi/frame_ktp.png new file mode 100644 index 00000000..5ee51d6c Binary files /dev/null and b/library/src/main/res/drawable-xxxhdpi/frame_ktp.png differ diff --git a/library/src/main/res/drawable-xxxhdpi/frame_npwp.png b/library/src/main/res/drawable-xxxhdpi/frame_npwp.png new file mode 100644 index 00000000..f8340720 Binary files /dev/null and b/library/src/main/res/drawable-xxxhdpi/frame_npwp.png differ diff --git a/library/src/main/res/drawable-xxxhdpi/frame_passport.png b/library/src/main/res/drawable-xxxhdpi/frame_passport.png new file mode 100644 index 00000000..42c8ad2f Binary files /dev/null and b/library/src/main/res/drawable-xxxhdpi/frame_passport.png differ diff --git a/library/src/main/res/drawable-xxxhdpi/frame_sim.png b/library/src/main/res/drawable-xxxhdpi/frame_sim.png new file mode 100644 index 00000000..18a98bf4 Binary files /dev/null and b/library/src/main/res/drawable-xxxhdpi/frame_sim.png differ diff --git a/library/src/main/res/drawable/frame_face.png b/library/src/main/res/drawable/frame_face.png deleted file mode 100644 index 60fb11d7..00000000 Binary files a/library/src/main/res/drawable/frame_face.png and /dev/null differ