android使用系统相机拍照返回照片,并通过webservice上传到服务器上

如果要在自己的应用中实现拍照的功能,首先要在AndroidManifest.xml文件中添加权限:

<uses-permission android:name="android.permission.CAMERA"/>  

启动相机的方法非常简单,通过intent访问MediaStore.ACTION_IMAGE_CAPTURE

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  // 指定拍照 

String name = new DateFormat().format("yyyyMMddhhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
imgName = name;
mFilePath += name;
Uri uri = Uri.fromFile(new File(mFilePath));// 加载路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);// 指定存储路径,这样就可以保存原图了

startActivityForResult(intent, 1);  // 拍照返回图片

拍照并确认后,Activity的onActivityResult方法会被调用,在这里可以获取图片的数据。

/**
     * 拍照返回
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            String sdStatus = Environment.getExternalStorageState();
            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
                Log.i("TestFile", "SD card is not avaiable/writeable right now.");
                return;
            }
            try {
                is = new FileInputStream(mFilePath);// 获取输入流
                Bitmap bitmap = BitmapFactory.decodeStream(is);// 把流解析成bitmap
                ViewGroup.LayoutParams lp;
                lp = img.getLayoutParams();
                lp.width = 300;
                lp.height = 300 * bitmap.getHeight() / bitmap.getWidth();
                img.setLayoutParams(lp);
                img.setImageBitmap(bitmap);// 将图片显示在ImageView里
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {// 关闭流
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

通过webservice上传图片

 /**
     * 上传图片
     */
    private String imgUpload() {
        try {
            FileInputStream fis = new FileInputStream(mFilePath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int count = 0;
            while ((count = fis.read(buffer)) >= 0) {
                baos.write(buffer, 0, count);
            }
            String uploadBuffer = new String(Base64.encode(baos.toByteArray()));  //进行Base64编码
            String f = connectWebService(uploadBuffer);
            Log.i("connectWebService", "start");
            fis.close();
            return f;
        } catch (Exception e) {
            e.printStackTrace();
            return "0";
        }
    }

    private String connectWebService(String imageBuffer) {
        SoapObject soapObject = new SoapObject(nameSpace, "uploadImage");
        soapObject.addProperty("flag", "1");            //标识
        soapObject.addProperty("filename", imgName);    // 图片名
        soapObject.addProperty("image", imageBuffer);   // 图片字符串
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.bodyOut = soapObject;
        envelope.setOutputSoapObject(soapObject);
        HttpTransportSE httpTranstation = new HttpTransportSE(wsdl);
        try {
            httpTranstation.call("", envelope);
            Object result = envelope.getResponse();
            Log.i("connectWebService", result.toString());
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "0";
        }
    }

转载于:https://my.oschina.net/yongqingfan/blog/724689

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值