android frame constraints

This commit is contained in:
Roman Kozlov 2018-04-05 17:16:47 +03:00
parent ee737eef51
commit d31df0677a
6 changed files with 88 additions and 133 deletions

View File

@ -2,10 +2,12 @@ package com.wix.RNCameraKit;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
@ -37,23 +39,30 @@ public class Utils {
return null;
}
public static @Nullable Integer getIntSafe(ReadableMap map, String key) {
@Nullable
public static Integer getIntSafe(ReadableMap map, String key) {
return getIntSafe(map, key, null);
}
public static Integer getIntSafe(ReadableMap map, String key, Integer defaultInt) {
if (map.hasKey(key)) {
return map.getInt(key);
}
return null;
return defaultInt;
}
public static @Nullable Boolean getBooleanSafe(ReadableMap map, String key) {
@Nullable
public static Boolean getBooleanSafe(ReadableMap map, String key) {
if (map.hasKey(key)) {
return map.getBoolean(key);
}
return null;
}
public static @NonNull ArrayList<String> readableArrayToList(ReadableArray items) {
@NonNull
public static ArrayList<String> readableArrayToList(ReadableArray items) {
ArrayList<String> list = new ArrayList<>();
for(int i = 0; i < items.size(); i++) {
for (int i = 0; i < items.size(); i++) {
list.add(items.getString(i));
}
return list;
@ -83,8 +92,8 @@ public class Utils {
WritableMap ans = Arguments.createMap();
ans.merge(image);
ans.putString("uri", FILE_PREFIX+resizedImagePath);
ans.putInt("size", (int)new File(resizedImagePath).length());
ans.putString("uri", FILE_PREFIX + resizedImagePath);
ans.putInt("size", (int) new File(resizedImagePath).length());
ans.putInt("width", scaledImage.getWidth());
ans.putInt("height", scaledImage.getHeight());
return ans;
@ -104,7 +113,7 @@ public class Utils {
float width = image.getWidth();
float height = image.getHeight();
float ratio = Math.min((float)maxWidth / width, (float)maxHeight / height);
float ratio = Math.min((float) maxWidth / width, (float) maxHeight / height);
int finalWidth = (int) (width * ratio);
int finalHeight = (int) (height * ratio);
@ -166,14 +175,14 @@ public class Utils {
* Loads the bitmap resource from the file specified in imagePath.
*/
private static Bitmap loadBitmapFromFile(Context context, String imagePath, int newWidth,
int newHeight) throws IOException {
int newHeight) throws IOException {
// Decode the image bounds to find the size of the source image.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
loadBitmap(context, imagePath, options);
// Set a sample size according to the image size to lower memory usage.
options.inSampleSize = calculateInSampleSize(options.outWidth, options.outHeight , newWidth, newHeight);
options.inSampleSize = calculateInSampleSize(options.outWidth, options.outHeight, newWidth, newHeight);
options.inJustDecodeBounds = false;
return loadBitmap(context, imagePath, options);
}
@ -190,7 +199,7 @@ public class Utils {
}
File newFile = new File(saveDirectory, fileName + "." + compressFormat.name());
if(!newFile.createNewFile()) {
if (!newFile.createNewFile()) {
throw new IOException("The file already exists");
}
@ -221,4 +230,9 @@ public class Utils {
public static void runOnWorkerThread(Runnable runnable) {
new Thread(runnable).start();
}
public static int convertDpToPx(int dp, Context context) {
Resources r = context.getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}
}

View File

@ -3,11 +3,13 @@ package com.wix.RNCameraKit.camera;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.ThemedReactContext;
import com.wix.RNCameraKit.Utils;
import com.wix.RNCameraKit.camera.barcode.BarcodeFrame;
@ -20,12 +22,13 @@ public class CameraView extends FrameLayout implements SurfaceHolder.Callback {
private boolean showFrame;
private Rect frameRect;
private BarcodeFrame barcodeFrame;
@ColorInt private int frameColor = Color.GREEN;
@ColorInt private int laserColor = Color.RED;
private int frameLeft;
private int frameTop;
private int frameWidth;
private int frameHeight;
// @ColorInt private int frameColor = Color.GREEN;
// @ColorInt private int laserColor = Color.RED;
// private int frameLeft;
// private int frameTop;
// private int frameWidth;
// private int frameHeight;
private ReadableMap scannerOptions;
public CameraView(ThemedReactContext context) {
super(context);
@ -88,9 +91,7 @@ public class CameraView extends FrameLayout implements SurfaceHolder.Callback {
public void showFrame() {
if (showFrame) {
barcodeFrame = new BarcodeFrame(getContext(), frameLeft, frameTop, frameHeight, frameWidth);
barcodeFrame.setFrameColor(frameColor);
barcodeFrame.setLaserColor(laserColor);
barcodeFrame = new BarcodeFrame(getContext(), scannerOptions);
addView(barcodeFrame);
requestLayout();
}
@ -120,20 +121,6 @@ public class CameraView extends FrameLayout implements SurfaceHolder.Callback {
return frameRect;
}
public void setFrameColor(@ColorInt int color) {
this.frameColor = color;
if (barcodeFrame != null) {
barcodeFrame.setFrameColor(color);
}
}
public void setLaserColor(@ColorInt int color) {
this.laserColor = color;
if (barcodeFrame != null) {
barcodeFrame.setLaserColor(laserColor);
}
}
/**
* Set background color for Surface view on the period, while camera is not loaded yet.
* Provides opportunity for user to hide period while camera is loading
@ -144,19 +131,7 @@ public class CameraView extends FrameLayout implements SurfaceHolder.Callback {
surface.setBackgroundColor(color);
}
public void setFrameLeft(int frameLeft) {
this.frameLeft = frameLeft;
}
public void setFrameTop(int frameTop) {
this.frameTop = frameTop;
}
public void setFrameWidth(int frameWidth) {
this.frameWidth = frameWidth;
}
public void setFrameHeight(int frameHeight) {
this.frameHeight = frameHeight;
public void setScannerOptions(ReadableMap scannerOptions) {
this.scannerOptions = scannerOptions;
}
}

View File

@ -14,6 +14,7 @@ import android.view.OrientationEventListener;
import android.view.WindowManager;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
@ -312,19 +313,14 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
}
}
@ReactProp(name = "showFrame", defaultBoolean = false)
@ReactProp(name = "showFrame")
public void setFrame(CameraView view, boolean show) {
view.setShowFrame(show);
}
@ReactProp(name = "frameColor", defaultInt = Color.GREEN)
public void setFrameColor(CameraView view, @ColorInt int color) {
view.setFrameColor(color);
}
@ReactProp(name = "laserColor", defaultInt = Color.RED)
public void setLaserColor(CameraView view, @ColorInt int color) {
view.setLaserColor(color);
@ReactProp(name = "scannerOptions")
public void setScannerOptions(CameraView view, ReadableMap options) {
view.setScannerOptions(options);
}
@ReactProp(name = "surfaceColor")
@ -332,26 +328,6 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
view.setSurfaceBgColor(color);
}
@ReactProp(name = "frameHeight")
public void setHeight(CameraView view, int height) {
view.setFrameHeight(height);
}
@ReactProp(name = "frameWidth")
public void setWidth(CameraView view, int width) {
view.setFrameWidth(width);
}
@ReactProp(name = "frameLeft")
public void setFrameTop(CameraView view, int left) {
view.setFrameLeft(left);
}
@ReactProp(name = "frameTop")
public void setFrameLeft(CameraView view, int top) {
view.setFrameTop(top);
}
public static synchronized Rect getFramingRectInPreview(int previewWidth, int previewHeight) {
return cameraViews.peek().getFramingRectInPreview(previewWidth, previewHeight);
}

View File

@ -2,21 +2,21 @@ package com.wix.RNCameraKit.camera.barcode;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.view.View;
import com.facebook.react.bridge.ReadableMap;
import com.wix.RNCameraKit.R;
import com.wix.RNCameraKit.Utils;
public class BarcodeFrame extends View {
private static final int STROKE_WIDTH = 5;
private static final int ANIMATION_SPEED = 8;
private static final int WIDTH_SCALE = 7;
private static final double HEIGHT_SCALE = 2.75;
private Paint dimPaint;
private Paint framePaint;
@ -30,12 +30,12 @@ public class BarcodeFrame extends View {
private long previousFrameTime = System.currentTimeMillis();
private int laserY;
public BarcodeFrame(Context context, int frameLeft, int frameTop, int frameHeight, int frameWidth) {
public BarcodeFrame(Context context, ReadableMap options) {
super(context);
init(context, frameLeft, frameTop, frameHeight, frameWidth);
init(context, options);
}
private void init(Context context, int frameLeft, int frameTop, int frameHeight, int frameWidth) {
private void init(Context context, ReadableMap options) {
framePaint = new Paint();
framePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
dimPaint = new Paint();
@ -49,11 +49,17 @@ public class BarcodeFrame extends View {
laserPaint.setStrokeWidth(STROKE_WIDTH);
frameRect = new Rect();
frameRect.left = frameLeft;
frameRect.top = frameTop;
frameRect.right = frameLeft + frameWidth;
frameRect.bottom = frameTop + frameHeight;
borderMargin = context.getResources().getDimensionPixelSize(R.dimen.border_length);
parseOptions(options);
}
private void parseOptions(ReadableMap options) {
frameRect.left = Utils.convertDpToPx(Utils.getIntSafe(options, "frameLeft", 0), getContext());
frameRect.top = Utils.convertDpToPx(Utils.getIntSafe(options, "frameTop", 0), getContext());
frameRect.right = frameRect.left + Utils.convertDpToPx(Utils.getIntSafe(options, "frameWidth", 0), getContext());
frameRect.bottom = frameRect.top + Utils.convertDpToPx(Utils.getIntSafe(options, "frameHeight", 0), getContext());
borderPaint.setColor(Utils.getIntSafe(options, "frameColor", Color.GREEN));
laserPaint.setColor(Utils.getIntSafe(options, "laserColor", Color.RED));
}
@Override
@ -62,13 +68,6 @@ public class BarcodeFrame extends View {
width = getMeasuredWidth();
height = getMeasuredHeight();
// int marginWidth = width / WIDTH_SCALE;
// int marginHeight = (int) (height / HEIGHT_SCALE);
//
// frameRect.left = marginWidth;
// frameRect.right = width - marginWidth;
// frameRect.top = marginHeight;
// frameRect.bottom = height - marginHeight;
}
@Override
@ -104,11 +103,4 @@ public class BarcodeFrame extends View {
return frameRect;
}
public void setFrameColor(@ColorInt int borderColor) {
borderPaint.setColor(borderColor);
}
public void setLaserColor(@ColorInt int laserColor) {
laserPaint.setColor(laserColor);
}
}

View File

@ -1,10 +1,17 @@
import React, { Component } from 'react';
import {
Alert
Alert,
Dimensions
} from 'react-native';
import { CameraKitCameraScreen } from '../../src';
import CheckingScreen from './CheckingScreen';
const { width, height } = Dimensions.get('window');
const frameleft = parseInt(width / 7);
const frameTop = parseInt(height / 2.75);
const frameWidth = width - 2 * frameleft;
const frameHeight = height - 2 * frameTop;
console.log('NIGA', `w=${width} h=${height} l=${frameleft} t=${frameTop}`);
export default class CameraScreen extends Component {
@ -46,20 +53,14 @@ export default class CameraScreen extends Component {
laserColor={"blue"}
surfaceColor={"black"}
frameColor={"yellow"}
frameTop={100}
frameLeft={100}
frameHeight={100}
frameWidth={100}
onReadCode={((event) => this.setState({ example: CheckingScreen }))}
hideControls={true}
colorForScannerFrame={'blue'}
frameHeight = {200}
frameWidth = {200}
frameLeft = {20}
frameTop = {300}
overlayColor = {'black'}
frameHeight={frameHeight}
frameWidth={frameWidth}
frameLeft={frameleft}
frameTop={frameTop}
overlayColor={'black'}
/>
);
}

View File

@ -9,7 +9,7 @@ import {
NativeModules,
Platform,
SafeAreaView,
processColor
processColor
} from 'react-native';
import _ from 'lodash';
import CameraKitCamera from './../CameraKitCamera';
@ -41,14 +41,14 @@ export default class CameraScreenBase extends Component {
mode: FLASH_MODE_AUTO,
image: _.get(this.props, 'flashImages.auto')
},
{
mode: FLASH_MODE_ON,
image: _.get(this.props, 'flashImages.on')
},
{
mode: FLASH_MODE_OFF,
image: _.get(this.props, 'flashImages.off')
}
{
mode: FLASH_MODE_ON,
image: _.get(this.props, 'flashImages.on')
},
{
mode: FLASH_MODE_OFF,
image: _.get(this.props, 'flashImages.off')
}
];
this.state = {
captureImages: [],
@ -58,7 +58,7 @@ export default class CameraScreenBase extends Component {
ratioArrayPosition: -1,
imageCaptured: undefined,
captured: false,
scannerOptions : {}
scannerOptions: {}
};
this.onSetFlash = this.onSetFlash.bind(this);
this.onSwitchCameraPressed = this.onSwitchCameraPressed.bind(this);
@ -130,7 +130,7 @@ export default class CameraScreenBase extends Component {
scannerOptions.laserColor = processColor('white')
}
if (this.props.colorForScannerFrame) {
if (this.props.frameColor) {
scannerOptions.colorForFrame = processColor(this.props.colorForScannerFrame);
} else {
scannerOptions.colorForFrame = processColor("white");
@ -169,10 +169,10 @@ export default class CameraScreenBase extends Component {
renderTopButtons() {
return !this.props.hideControls && (
<SafeAreaView style={styles.topButtons}>
{this.renderFlashButton()}
{this.renderSwitchCameraButton()}
</SafeAreaView>
<SafeAreaView style={styles.topButtons}>
{this.renderFlashButton()}
{this.renderSwitchCameraButton()}
</SafeAreaView>
);
}
@ -191,11 +191,8 @@ export default class CameraScreenBase extends Component {
cameraOptions={this.state.cameraOptions}
showFrame={this.props.showFrame}
scanBarcode={this.props.scanBarcode}
laserColor={this.props.laserColor}
frameColor={this.props.frameColor}
surfaceColor={this.props.surfaceColor}
onReadCode = {this.props.onReadCode}
scannerOptions = {this.state.scannerOptions}
onReadCode={this.props.onReadCode}
scannerOptions={this.state.scannerOptions}
/>
}
</View>