Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android如何从imageview转换条形码并从收据打印机打印_Android_Imageview_Barcode_Thermal Printer - Fatal编程技术网

Android如何从imageview转换条形码并从收据打印机打印

Android如何从imageview转换条形码并从收据打印机打印,android,imageview,barcode,thermal-printer,Android,Imageview,Barcode,Thermal Printer,我已将imageview转换为字节格式,但没有从收据打印机打印出条形码。有什么建议可以解决我的问题,或者有其他方法可以用收据打印机打印条形码 这就是我如何将imageview转换为byte并使用OutputStream打印条形码的方法 ImageView iv = (ImageView) findViewById(R.id.imageView); // barcode data String barcode_data = "asdasdasd";

我已将imageview转换为字节格式,但没有从收据打印机打印出条形码。有什么建议可以解决我的问题,或者有其他方法可以用收据打印机打印条形码

这就是我如何将imageview转换为byte并使用OutputStream打印条形码的方法

       ImageView iv = (ImageView) findViewById(R.id.imageView);
       // barcode data
       String barcode_data = "asdasdasd";
       OutputStream socketOut = null;
       // barcode image
       Bitmap bitmap = null;

       try {

             bitmap = encodeAsBitmap(barcode_data, BarcodeFormat.CODE_128, 1000, 1000);
             iv.setImageBitmap(bitmap);
             bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
             byte[] imageInByte = baos.toByteArray();
             try {
                    socketOut.write(imageInByte);
                 } catch (IOException e) {
                    e.printStackTrace();
                 }


             } catch (WriterException e) {
               e.printStackTrace();
           }

Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException {
    String contentsToEncode = contents;
    if (contentsToEncode == null) {
        return null;
    }
    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contentsToEncode);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result;
    try {
        result = writer.encode(contentsToEncode, format, img_width, img_height, hints);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

private static String guessAppropriateEncoding(CharSequence contents) {
    // Very crude at the moment
    for (int i = 0; i < contents.length(); i++) {
        if (contents.charAt(i) > 0xFF) {
            return "UTF-8";
        }
    }
    return null;
}
ImageView iv=(ImageView)findViewById(R.id.ImageView);
//条形码数据
字符串条形码_data=“asdasdasd”;
OutputStream socketOut=null;
//条码图像
位图=空;
试一试{
位图=encodeAsBitmap(条形码数据,条形码格式.CODE_128,1000,1000);
iv.设置图像位图(位图);
位图=((BitmapDrawable)iv.getDrawable()).getBitmap();
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
压缩(bitmap.CompressFormat.JPEG,100,baos);
字节[]imageInByte=bas.toByteArray();
试一试{
socketOut.write(imageInByte);
}捕获(IOE异常){
e、 printStackTrace();
}
}捕获(写入异常e){
e、 printStackTrace();
}
位图encodeAsBitmap(字符串内容、条形码格式、int-img\u宽度、int-img\u高度)引发WriterException{
字符串contentsToEncode=内容;
if(contentsToEncode==null){
返回null;
}
映射提示=null;
字符串编码=猜测适当编码(contentsToEncode);
if(编码!=null){
提示=新的EnumMap(EncodeHintType.class);
put(EncodeHintType.CHARACTER\u集,编码);
}
MultiFormatWriter writer=新的MultiFormatWriter();
位矩阵结果;
试一试{
结果=writer.encode(内容编码、格式、img\u宽度、img\u高度、提示);
}捕获(IllegalArgumentException iae){
//不支持的格式
返回null;
}
int width=result.getWidth();
int height=result.getHeight();
int[]像素=新int[宽度*高度];
对于(int y=0;y0xFF){
返回“UTF-8”;
}
}
返回null;
}

您找到解决方案了吗?您找到解决方案了吗?