如果要在自己的应用中实现拍照的功能,首先要在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";
}
}