Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Barcode not display on Android #35

Open
jason7toxic opened this issue Nov 25, 2018 · 9 comments
Open

Barcode not display on Android #35

jason7toxic opened this issue Nov 25, 2018 · 9 comments

Comments

@jason7toxic
Copy link

jason7toxic commented Nov 25, 2018

Anyone having this issue?

import Barcode from "react-native-barcode-builder";

<Barcode value="1OpzLsiooEe3ac786JY2nQ==" text="1OpzLsiooEe3ac786JY2nQ==" format="CODE128" width={1} height={100} />

screenshot_1543159649

@inconspicuous-username
Copy link

inconspicuous-username commented Jan 13, 2019

I have the same issue on my android devices (Nexus 7, Xiaomi mi5s plus, and oneplus 5) using Expo.
It occurs even when I copy all the source files from the included example in an new Expo project.
It might be some Expo related issue as the examples aren't an expo project, but I'm not proficient enough to try it out (First time ever using react-native).

@lorenamelor
Copy link

same problem here

@ntnamag
Copy link

ntnamag commented May 7, 2019

I copy and edit using Svg. You can preview my code. Hope can help you

import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';

import barcodes from 'jsbarcode/src/barcodes';
import {Path, Svg} from 'react-native-svg';

export default class BarCode extends Component {
    static defaultProps = {
        value: undefined,
        format: 'CODE128',
        text: undefined,
        width: 2,
        height: 100,
        lineColor: '#000000',
        textColor: '#000000',
        background: '#ffffff',
        onError: undefined
    };

    constructor(props) {
        super(props);
        this.state = {
            bars: [],
            barCodeWidth: 0
        };
    }

    componentDidMount() {
        this.update();
    }

    componentDidUpdate() {
        this.update();
    }

    update() {
        const encoder = barcodes[this.props.format];
        const encoded = this.encode(this.props.value, encoder, this.props);

        if (encoded) {
            this.state.bars = this.drawSvgBarCode(encoded, this.props);
            this.state.barCodeWidth = encoded.data.length * this.props.width;
        }
    }

    drawSvgBarCode(encoding, options = {}) {
        const rects = [];
        // binary data of barcode
        const binary = encoding.data;

        let barWidth = 0;
        let x = 0;
        let yFrom = 0;
        // alert(JSON.stringify(options));

        for (let b = 0; b < binary.length; b++) {
            x = b * options.width;
            if (binary[b] === '1') {
                barWidth++;
            } else if (barWidth > 0) {
                rects[rects.length] = this.drawRect(
                    x - options.width * barWidth,
                    yFrom,
                    options.width * barWidth,
                    options.height
                );
                barWidth = 0;
            }
        }

        // Last draw is needed since the barcode ends with 1
        if (barWidth > 0) {
            rects[rects.length] = this.drawRect(
                x - options.width * (barWidth - 1),
                yFrom,
                options.width * barWidth,
                options.height
            );
        }

        return rects;
    }

    drawRect(x, y, width, height) {
        return `M${x},${y}h${width}v${height}h-${width}z`;
    }

    getTotalWidthOfEncodings(encodings) {
        let totalWidth = 0;
        for (let i = 0; i < encodings.length; i++) {
            totalWidth += encodings[i].width;
        }
        return totalWidth;
    }

    // encode() handles the Encoder call and builds the binary string to be rendered
    encode(text, Encoder, options) {
        // Ensure that text is a string
        text = '' + text;

        var encoder;

        try {
            encoder = new Encoder(text, options);
        } catch (error) {
            // If the encoder could not be instantiated, throw error.
            if (this.props.onError) {
                this.props.onError(new Error('Invalid barcode format.'));
                return;
            } else {
                throw new Error('Invalid barcode format.');
            }
        }

        // If the input is not valid for the encoder, throw error.
        if (!encoder.valid()) {
            if (this.props.onError) {
                this.props.onError(new Error('Invalid barcode for selected format.'));
                return;
            } else {
                throw new Error('Invalid barcode for selected format.');
            }
        }

        // Make a request for the binary data (and other infromation) that should be rendered
        // encoded stucture is {
        //  text: 'xxxxx',
        //  data: '110100100001....'
        // }
        var encoded = encoder.encode();

        return encoded;
    }

    render() {
        this.update();
        const backgroundStyle = {
            backgroundColor: this.props.background
        };
        return (
            <View style={[styles.svgContainer, backgroundStyle]}>
                <Svg height={this.props.height} width={this.state.barCodeWidth}>
                    <Path d={this.state.bars.join(",")} stroke={this.props.lineColor}/>
                </Svg>
                {typeof (this.props.text) != 'undefined' &&
                <Text style={{
                    color: this.props.textColor,
                    width: this.state.barCodeWidth,
                    textAlign: 'center'
                }}>{this.props.text}</Text>
                }
            </View>
        );
    }
}

const styles = StyleSheet.create({
    svgContainer: {
        alignItems: 'center',
        padding: 10
    }
});

@pouyaemami
Copy link

This issue happens for me on Android when I render the barcode off-screen and then bring it into view. It doesn't happen when I render the barcode on-screen on mount.

@zausailov
Copy link

I copy and edit using Svg. You can preview my code. Hope can help you

<Svg height={this.props.height} width={this.state.barCodeWidth}>
       <Path d={this.state.bars.join(",")} stroke={this.props.lineColor}/>
</Svg>
});

Works for me. Thx very much.

@dickwyn
Copy link

dickwyn commented Aug 30, 2019

Ran into an issue with @ntnamag's code. Modified the following lines and it worked.

<Svg height={this.props.height} width={this.state.barCodeWidth}>
   <Path d={this.state.bars.join(' ')} stroke={this.props.lineColor} />
</Svg>

Thanks!

@pouyaemami
Copy link

Would someone be able to open a PR for this?

@SahanDll
Copy link

Anyone found a solution for this?

@SahanDll
Copy link

Use below code to identify the foreground event and refresh your data.

import React, { Component } from "react";
import { AppState, StyleSheet, Text, View } from "react-native";

class AppStateExample extends Component {
state = {
appState: AppState.currentState
};

componentDidMount() {
AppState.addEventListener("change", this._handleAppStateChange);
}

componentWillUnmount() {
AppState.removeEventListener("change", this._handleAppStateChange);
}

_handleAppStateChange = nextAppState => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("App has come to the foreground!");
}
this.setState({ appState: nextAppState });
};

render() {
return (

Current state is: {this.state.appState}

);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center"
}
});

export default AppStateExample;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants