Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy-budiyev committed May 21, 2022
1 parent 1966ad7 commit 41252df
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 49 deletions.
186 changes: 137 additions & 49 deletions src/main/java/com/budiyev/android/codescanner/CodeScannerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.LayoutDirection;
Expand Down Expand Up @@ -83,11 +84,15 @@ public final class CodeScannerView extends ViewGroup {
private int mAutoFocusButtonPaddingHorizontal;
private int mAutoFocusButtonPaddingVertical;
private int mAutoFocusButtonColor;
private Drawable mAutoFocusButtonOnIcon;
private Drawable mAutoFocusButtonOffIcon;
private ImageView mFlashButton;
private ButtonPosition mFlashButtonPosition;
private int mFlashButtonPaddingHorizontal;
private int mFlashButtonPaddingVertical;
private int mFlashButtonColor;
private Drawable mFlashButtonOnIcon;
private Drawable mFlashButtonOffIcon;
private Point mPreviewSize;
private SizeListener mSizeListener;
private CodeScanner mCodeScanner;
Expand Down Expand Up @@ -145,11 +150,9 @@ private void initialize(@NonNull final Context context, @Nullable final Attribut
mFocusAreaSize = Math.round(density * FOCUS_AREA_SIZE_DP);
mAutoFocusButton = new ImageView(context);
mAutoFocusButton.setScaleType(ImageView.ScaleType.CENTER);
mAutoFocusButton.setImageResource(R.drawable.ic_code_scanner_auto_focus_on);
mAutoFocusButton.setOnClickListener(new AutoFocusClickListener());
mFlashButton = new ImageView(context);
mFlashButton.setScaleType(ImageView.ScaleType.CENTER);
mFlashButton.setImageResource(R.drawable.ic_code_scanner_flash_on);
mFlashButton.setOnClickListener(new FlashClickListener());
if (attrs == null) {
mViewFinderView.setFrameAspectRatio(DEFAULT_FRAME_ASPECT_RATIO_WIDTH,
Expand All @@ -176,6 +179,12 @@ private void initialize(@NonNull final Context context, @Nullable final Attribut
defaultButtonPadding, defaultButtonPadding);
mFlashButton.setPadding(defaultButtonPadding, defaultButtonPadding,
defaultButtonPadding, defaultButtonPadding);
mAutoFocusButtonOnIcon =
Utils.getDrawable(context, R.drawable.ic_code_scanner_auto_focus_on);
mAutoFocusButtonOffIcon =
Utils.getDrawable(context, R.drawable.ic_code_scanner_auto_focus_off);
mFlashButtonOnIcon = Utils.getDrawable(context, R.drawable.ic_code_scanner_flash_on);
mFlashButtonOffIcon = Utils.getDrawable(context, R.drawable.ic_code_scanner_flash_off);
} else {
TypedArray a = null;
try {
Expand Down Expand Up @@ -215,6 +224,14 @@ private void initialize(@NonNull final Context context, @Nullable final Attribut
setAutoFocusButtonPaddingVertical(a.getDimensionPixelOffset(
R.styleable.CodeScannerView_autoFocusButtonPaddingVertical,
defaultButtonPadding));
final Drawable autoFocusButtonOnIcon =
a.getDrawable(R.styleable.CodeScannerView_autoFocusButtonOnIcon);
setAutoFocusButtonOnIcon(autoFocusButtonOnIcon != null ? autoFocusButtonOnIcon :
Utils.getDrawable(context, R.drawable.ic_code_scanner_auto_focus_on));
final Drawable autoFocusButtonOffIcon =
a.getDrawable(R.styleable.CodeScannerView_autoFocusButtonOffIcon);
setAutoFocusButtonOffIcon(autoFocusButtonOffIcon != null ? autoFocusButtonOffIcon :
Utils.getDrawable(context, R.drawable.ic_code_scanner_auto_focus_off));
setFlashButtonVisible(a.getBoolean(R.styleable.CodeScannerView_flashButtonVisible,
DEFAULT_FLASH_BUTTON_VISIBLE));
setFlashButtonColor(a.getColor(R.styleable.CodeScannerView_flashButtonColor,
Expand All @@ -228,12 +245,24 @@ private void initialize(@NonNull final Context context, @Nullable final Attribut
setFlashButtonPaddingVertical(a.getDimensionPixelOffset(
R.styleable.CodeScannerView_flashButtonPaddingVertical,
defaultButtonPadding));
final Drawable flashButtonOnIcon =
a.getDrawable(R.styleable.CodeScannerView_flashButtonOnIcon);
setFlashButtonOnIcon(flashButtonOnIcon != null ? flashButtonOnIcon :
Utils.getDrawable(context, R.drawable.ic_code_scanner_flash_on));
final Drawable flashButtonOffIcon =
a.getDrawable(R.styleable.CodeScannerView_flashButtonOffIcon);
setFlashButtonOffIcon(flashButtonOffIcon != null ? flashButtonOffIcon :
Utils.getDrawable(context, R.drawable.ic_code_scanner_flash_off));
} finally {
if (a != null) {
a.recycle();
}
}
}
if (isInEditMode()) {
setAutoFocusEnabled(true);
setFlashEnabled(true);
}
addView(mPreviewView,
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(mViewFinderView,
Expand Down Expand Up @@ -313,49 +342,6 @@ protected void onLayout(final boolean changed, final int left, final int top, fi
}
}

private void layoutButton(final View button, final ButtonPosition position,
final int parentWidth, final int parentHeight) {
final int width = button.getMeasuredWidth();
final int height = button.getMeasuredHeight();
final int layoutDirection = getLayoutDirection();
switch (position) {
case TOP_START: {
if (layoutDirection == LayoutDirection.RTL) {
button.layout(parentWidth - width, 0, parentWidth, height);
} else {
button.layout(0, 0, width, height);
}
break;
}
case TOP_END: {
if (layoutDirection == LayoutDirection.RTL) {
button.layout(0, 0, width, height);
} else {
button.layout(parentWidth - width, 0, parentWidth, height);
}
break;
}
case BOTTOM_START: {
if (layoutDirection == LayoutDirection.RTL) {
button.layout(parentWidth - width, parentHeight - height, parentWidth,
parentHeight);
} else {
button.layout(0, parentHeight - height, width, parentHeight);
}
break;
}
case BOTTOM_END: {
if (layoutDirection == LayoutDirection.RTL) {
button.layout(0, parentHeight - height, width, parentHeight);
} else {
button.layout(parentWidth - width, parentHeight - height, parentWidth,
parentHeight);
}
break;
}
}
}

@Override
protected void onSizeChanged(final int width, final int height, final int oldWidth,
final int oldHeight) {
Expand Down Expand Up @@ -760,6 +746,36 @@ public boolean isFlashButtonVisible() {
return mFlashButton.getVisibility() == VISIBLE;
}

@NonNull
public Drawable getAutoFocusButtonOnIcon() {
return mAutoFocusButtonOnIcon;
}

public void setAutoFocusButtonOnIcon(@NonNull final Drawable icon) {
Objects.requireNonNull(icon);
final boolean changed = icon != mAutoFocusButtonOnIcon;
mAutoFocusButtonOnIcon = icon;
final CodeScanner codeScanner = mCodeScanner;
if (changed && codeScanner != null) {
setAutoFocusEnabled(codeScanner.isAutoFocusEnabled());
}
}

@NonNull
public Drawable getAutoFocusButtonOffIcon() {
return mAutoFocusButtonOffIcon;
}

public void setAutoFocusButtonOffIcon(@NonNull final Drawable icon) {
Objects.requireNonNull(icon);
final boolean changed = icon != mAutoFocusButtonOffIcon;
mAutoFocusButtonOffIcon = icon;
final CodeScanner codeScanner = mCodeScanner;
if (changed && codeScanner != null) {
setAutoFocusEnabled(codeScanner.isAutoFocusEnabled());
}
}

/**
* Set whether flash button is visible or not
*
Expand Down Expand Up @@ -865,6 +881,36 @@ public void setFlashButtonPaddingVertical(@Px final int padding) {
}
}

@NonNull
public Drawable getFlashButtonOnIcon() {
return mFlashButtonOnIcon;
}

public void setFlashButtonOnIcon(@NonNull final Drawable icon) {
Objects.requireNonNull(icon);
final boolean changed = icon != mFlashButtonOnIcon;
mFlashButtonOnIcon = icon;
final CodeScanner codeScanner = mCodeScanner;
if (changed && codeScanner != null) {
setFlashEnabled(codeScanner.isFlashEnabled());
}
}

@NonNull
public Drawable getFlashButtonOffIcon() {
return mFlashButtonOffIcon;
}

public void setFlashButtonOffIcon(@NonNull final Drawable icon) {
Objects.requireNonNull(icon);
final boolean changed = icon != mFlashButtonOffIcon;
mFlashButtonOffIcon = icon;
final CodeScanner codeScanner = mCodeScanner;
if (changed && codeScanner != null) {
setFlashEnabled(codeScanner.isFlashEnabled());
}
}

@NonNull
SurfaceView getPreviewView() {
return mPreviewView;
Expand Down Expand Up @@ -899,13 +945,55 @@ void setCodeScanner(@NonNull final CodeScanner codeScanner) {
}

void setAutoFocusEnabled(final boolean enabled) {
mAutoFocusButton.setImageResource(enabled ? R.drawable.ic_code_scanner_auto_focus_on :
R.drawable.ic_code_scanner_auto_focus_off);
mAutoFocusButton.setImageDrawable(
enabled ? mAutoFocusButtonOnIcon : mAutoFocusButtonOffIcon);
}

void setFlashEnabled(final boolean enabled) {
mFlashButton.setImageResource(enabled ? R.drawable.ic_code_scanner_flash_on :
R.drawable.ic_code_scanner_flash_off);
mFlashButton.setImageDrawable(enabled ? mFlashButtonOnIcon : mFlashButtonOffIcon);
}

private void layoutButton(final View button, final ButtonPosition position,
final int parentWidth, final int parentHeight) {
final int width = button.getMeasuredWidth();
final int height = button.getMeasuredHeight();
final int layoutDirection = getLayoutDirection();
switch (position) {
case TOP_START: {
if (layoutDirection == LayoutDirection.RTL) {
button.layout(parentWidth - width, 0, parentWidth, height);
} else {
button.layout(0, 0, width, height);
}
break;
}
case TOP_END: {
if (layoutDirection == LayoutDirection.RTL) {
button.layout(0, 0, width, height);
} else {
button.layout(parentWidth - width, 0, parentWidth, height);
}
break;
}
case BOTTOM_START: {
if (layoutDirection == LayoutDirection.RTL) {
button.layout(parentWidth - width, parentHeight - height, parentWidth,
parentHeight);
} else {
button.layout(0, parentHeight - height, width, parentHeight);
}
break;
}
case BOTTOM_END: {
if (layoutDirection == LayoutDirection.RTL) {
button.layout(0, parentHeight - height, width, parentHeight);
} else {
button.layout(parentWidth - width, parentHeight - height, parentWidth,
parentHeight);
}
break;
}
}
}

private void invalidateAutoFocusButtonPadding() {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/budiyev/android/codescanner/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.hardware.Camera.Area;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.os.Build;
import android.view.Surface;
import android.view.WindowManager;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.zxing.BinaryBitmap;
Expand Down Expand Up @@ -352,6 +355,17 @@ public void onError(@NonNull final Throwable thrown) {
}
}

@NonNull
@SuppressWarnings("deprecation")
public static Drawable getDrawable(@NonNull final Context context,
@DrawableRes final int resId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return context.getDrawable(resId);
} else {
return context.getResources().getDrawable(resId);
}
}

private static int mapCoordinate(final int value, final int size) {
return 2000 * value / size - 1000;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<attr name="autoFocusButtonVisible" format="boolean"/>
<attr name="autoFocusButtonPaddingHorizontal" format="dimension"/>
<attr name="autoFocusButtonPaddingVertical" format="dimension"/>
<attr name="autoFocusButtonOnIcon" format="reference"/>
<attr name="autoFocusButtonOffIcon" format="reference"/>
<attr name="autoFocusButtonPosition" format="enum">
<enum name="topStart" value="0"/>
<enum name="topEnd" value="1"/>
Expand All @@ -24,6 +26,8 @@
<attr name="flashButtonVisible" format="boolean"/>
<attr name="flashButtonPaddingHorizontal" format="dimension"/>
<attr name="flashButtonPaddingVertical" format="dimension"/>
<attr name="flashButtonOnIcon" format="reference"/>
<attr name="flashButtonOffIcon" format="reference"/>
<attr name="flashButtonPosition" format="enum">
<enum name="topStart" value="0"/>
<enum name="topEnd" value="1"/>
Expand Down

0 comments on commit 41252df

Please sign in to comment.