如何将字符串转换为int?

时间:2015-08-19 08:31:16

标签: java android

对此可能有一个非常简单的答案。我的目标是将输入的信息x和y除以并将其放入z。

这是我的代码:

#!/usr/bin/env python

import numpy as np
import cv2

help_message = '''
USAGE: peopledetect.py <image_names> ...

Press any key to continue, ESC to stop.
'''

def inside(r, q):
    rx, ry, rw, rh = r
    qx, qy, qw, qh = q
    return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh

def draw_detections(img, rects, thickness = 1):
    for x, y, w, h in rects:
        # the HOG detector returns slightly larger rectangles than the real objects.
        # so we slightly shrink the rectangles to get a nicer output.
        pad_w, pad_h = int(0.15*w), int(0.05*h)
        cv2.rectangle(img, (x+pad_w, y+pad_h), (x+w-pad_w, y+h-pad_h), (0, 255, 0), thickness)


if __name__ == '__main__':
    import sys
    from glob import glob
    import itertools as it

    print help_message

    hog = cv2.HOGDescriptor()
    hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() )
    print "hi"
    for fn in it.chain(*map(glob, sys.argv[1:])):
        print fn, ' - ',
        try:
            img = cv2.imread(fn)
            '''rows,cols,z = img.shape
            print rows,cols
            rows = float(rows)
            cols = float(cols)
            #img = cv2.resize(img,None,fx=384.0/rows, fy=256.0/cols, interpolation = cv2.INTER_CUBIC)
            print img.shape'''
        except:
            print 'loading error'
            continue

        found, w = hog.detectMultiScale(img, winStride=(100,100), padding=(32,32), scale=1.05)
        found_filtered = []
        for ri, r in enumerate(found):
            for qi, q in enumerate(found):
                if ri != qi and inside(r, q):
                    break
            else:
                found_filtered.append(r)
        draw_detections(img, found)
        draw_detections(img, found_filtered, 3)
        print '%d (%d) found' % (len(found_filtered), len(found))
        cv2.imshow('img', img)
        ch = 0xFF & cv2.waitKey()
        if ch == 27:
            break
    cv2.destroyAllWindows()

2 个答案:

答案 0 :(得分:1)

z函数中声明onclick。或者在函数内部赋值。

另外nopcob是editText获取其中的数据,请使用getText()

由于Integer.parseInt()只接受String而不接受editText,因此您必须使用getText()来获取其中的字符串值。

x = Integer.parseInt(nop.getText().toString());
y = Integer.parseInt(cob.getText().toString());
 final TextView tv = (TextView) findViewById(R.id.textView);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          int z = x / y;
          tv.setText(z);
        }
    });

此外,z全局声明并且未分配任何内容。但是,当您单击按钮时需要为其分配值,以便每次单击按钮时editText都会更改。

答案 1 :(得分:0)

首先从Edittext转换为字符串,然后将其设为Integer。

String Val1 = nop.getText().toString();
String Val2 =cob.getText().toString();
x = Integer.parseInt(val1);
y = Integer.parseInt(val2);