Android中调用设备的照相和摄像功能

本文介绍了一个用于执法活动中记录企业违规行为的应用程序开发细节。该应用支持拍摄照片和视频作为证据,能够通过ArcGISFeatureLayer与地图服务交互,并实现了数据本地存储及同步功能。
package com.esri.activity; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.app.ProgressDialog; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout.LayoutParams; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import com.esri.android.map.ags.ArcGISFeatureLayer; import com.esri.android.map.ags.ArcGISFeatureLayer.MODE; import com.esri.core.geometry.Point; import com.esri.core.map.CallbackListener; import com.esri.core.map.FeatureEditResult; import com.esri.core.map.FeatureSet; import com.esri.core.map.Graphic; import com.esri.core.tasks.ags.query.Query; import com.esri.core.tasks.ags.query.QueryTask; import com.esri.geometry.utils.GeometryUtils; import com.esri.modle.Enterprise; import com.esri.modle.LawEnforcement; import com.esri.sql.MySQLiteHelper; import com.esri.utils.FeatureUtils; import com.esri.utils.FileOpreateUtils; public class LawEnforcementActivity extends Activity { public static final int NONE = 0; public static final int PHOTOHRAPH = 1;// 拍照 public static final int PHOTOZOOM = 2; // 缩放 public static final int PHOTORESOULT = 3;// 结果 public static final int CODE_VIDEO = 4;//录像 public static final String IMAGE_UNSPECIFIED = "image/*"; LawEnforcement law = null; String filePhotoName = ""; String fileVideoName = ""; Intent intent = null; Spinner law_spinner = null; EditText edit_Enterprisename; EditText enterpris_remarksEdit; ImageView imageView = null; boolean isform = false; ArcGISFeatureLayer featureLayer = null; private MySQLiteHelper msh = null;//数据库操作类 ProgressDialog prog; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.law_enforcement); featureLayer = new ArcGISFeatureLayer(FeatureUtils.LAWENFORCEMENT_URL, MODE.SELECTION); law_spinner = (Spinner)findViewById(R.id.law_spinner); edit_Enterprisename = (EditText)findViewById(R.id.edit_Enterprisename); enterpris_remarksEdit = (EditText)findViewById(R.id.enterpris_remarksEdit); imageView = (ImageView)findViewById(R.id.imgView); setValues(); setEdites(); msh = new MySQLiteHelper(this,"YDZF"); } public void onLocation(View v){ law.setEnterpris_illegal_type(((TextView)law_spinner.getSelectedView()).getText().toString()); law.setEnterpris_Name(edit_Enterprisename.getText().toString()); law.setRemarks(enterpris_remarksEdit.getText().toString()); law.setFilePhotoName(filePhotoName); law.setFileVideoName(fileVideoName); Bundle bd = new Bundle(); bd.putSerializable("lawenforcement", law); intent.putExtras(bd); setResult(1003,intent); finish(); } private void setValues(){ ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.violatetypes, android.R.layout.simple_list_item_1); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); law_spinner.setAdapter(spinnerAdapter); law_spinner.setPromptId(R.string.enterpriseviolatetypes); law_spinner.setOnItemSelectedListener(spinnerListener); intent = getIntent(); Bundle bd = intent.getExtras(); law = (LawEnforcement)bd.getSerializable("lawenforcement"); if(intent.getSerializableExtra("geo") != null){ Point geo = (Point)intent.getSerializableExtra("geo"); law.setGeo(geo); } //设置spinner的选项 law_spinner.setSelection(getSelectCode(law.getEnterpris_illegal_type(),spinnerAdapter)); edit_Enterprisename.setText(law.getEnterpris_Name()); enterpris_remarksEdit.setText(law.getRemarks()); filePhotoName = law.getFilePhotoName(); if(filePhotoName != null && !filePhotoName.equals("")){ Bitmap img = BitmapFactory.decodeFile(filePhotoName); imageView.setImageBitmap(img); imageView.setLayoutParams(new LayoutParams(100, 100)); } } private int getSelectCode(String name,ArrayAdapter<CharSequence> temp){ int postion=0; if(temp == null){ return 0; } for(int i=0;i<temp.getCount();i++){ if(name.equalsIgnoreCase(temp.getItem(i).toString())){ postion = i; break; } } return postion; } private AdapterView.OnItemSelectedListener spinnerListener =new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View v, int arg2, long arg3) { // TODO Auto-generated method stub TextView tv = (TextView)v; tv.setTextColor(getResources().getColor(R.color.black)); tv.setTextSize(12); tv.setGravity(Gravity.CENTER_HORIZONTAL); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }; public void doTakePhoto(View v){ filePhotoName = Environment.getExternalStorageDirectory ().getAbsolutePath()+"/MyMediaStore/"; File out = new File(filePhotoName); if(!out.exists()){ out.mkdirs(); } filePhotoName = filePhotoName+new Date().getTime()+".jpg"; out = new File(filePhotoName); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out)); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); startActivityForResult(intent, PHOTOHRAPH); } public void doTakeVideo(View v) throws IOException{ fileVideoName = Environment.getExternalStorageDirectory ().getAbsolutePath()+"/MyMediaStore/"; Intent videoCaptureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); // File out = new File(fileVideoName); // if(!out.exists()){ // out.mkdirs(); // } // out = new File(fileVideoName, new Date().getTime()+".mp4"); // if(!out.exists()){ // out.createNewFile(); // } // videoCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out)); videoCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); videoCaptureIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,"2000"); startActivityForResult(videoCaptureIntent, CODE_VIDEO); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return; // 拍照 if (requestCode == PHOTOHRAPH) { //设置文件保存路径这里放在跟目录下 // File picture = new File(filePhotoName); // startPhotoZoom(Uri.fromFile(picture)); Bitmap img = BitmapFactory.decodeFile(filePhotoName); imageView.setImageBitmap(img); imageView.setLayoutParams(new LayoutParams(100, 100)); } if (data == null) return; // 读取相册缩放图片 if (requestCode == PHOTOZOOM) { startPhotoZoom(data.getData()); } // 处理结果 if (requestCode == PHOTORESOULT) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件 imageView.setImageBitmap(photo); } } if(requestCode == CODE_VIDEO) { Uri uri = data.getData(); String[] proj = { MediaStore.Video.Media.DATA }; Cursor actualimagecursor = managedQuery(uri,proj,null,null,null); int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); actualimagecursor.moveToFirst(); String img_path = actualimagecursor.getString(actual_image_column_index); File file = new File(img_path); fileVideoName = Environment.getExternalStorageDirectory ().getAbsolutePath()+"/MyMediaStore/"; File out = new File(fileVideoName); if(!out.exists()){ out.mkdirs(); } out = new File(fileVideoName, new Date().getTime()+".mp4"); FileOpreateUtils.copyfile(file, out, true); if(file.exists()){ file.delete(); } // if(!out.exists()){ // try { // out.createNewFile(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // // FileOutputStream outStream = null; // try { // outStream = new FileOutputStream(out); // outStream.write(videoBytes); // outStream.close(); // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } } super.onActivityResult(requestCode, resultCode, data); } public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 64); intent.putExtra("outputY", 64); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTORESOULT); } public void editForm(View v){ setEdites(); } private void setEdites(){ law_spinner.setEnabled(isform); //edit_Enterprisename.setEnabled(isform); enterpris_remarksEdit.setEnabled(isform); if(isform){ isform =false; }else{ isform = true; } } public void submitBtn(View v){ Button btn = (Button)v; btn.setEnabled(false); if(law.getGeo() == null){ Toast.makeText(LawEnforcementActivity.this, "没有定位信息,请先定位再提交!", Toast.LENGTH_LONG).show(); return; } SQLiteDatabase db = msh.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("OBJECTID", law.getObjectid()); values.put("ENTERPRIS_NAME", edit_Enterprisename.getText().toString()); values.put("ENTERPRIS_ILLEGAL_TYPE",((TextView)law_spinner.getSelectedView()).getText().toString()); values.put("EXAMINE_PERSONNAME", law.getExamine_personName()); values.put("EQUIPMENT_NUMBER", law.getEquipment_number()); values.put("REMARKS",enterpris_remarksEdit.getText().toString()); values.put("FILEPHOTONAME",filePhotoName); values.put("FILEVIDEONAME",fileVideoName); String shape = GeometryUtils.GeometryToWKT(law.getGeo()); values.put("SHAPE", shape); values.put("MARK", 1); db.update(MySQLiteHelper.LAW_ENFOREMENT, values, "OBJECTID=?", new String[]{law.getObjectid().toString()}); Map<String,Object> attrs = new HashMap<String, Object>(); attrs.put("OBJECTID", law.getObjectid()); attrs.put("ENTERPRIS_NAME", edit_Enterprisename.getText().toString()); attrs.put("ENTERPRIS_ILLEGAL_TYPE", ((TextView)law_spinner.getSelectedView()).getText().toString()); attrs.put("EXAMINE_PERSONNAME", law.getExamine_personName()); attrs.put("EQUIPMENT_NUMBER",law.getEquipment_number()); attrs.put("REMARKS", enterpris_remarksEdit.getText().toString()); if(law.getGeo() != null){ Point pt = (Point)law.getGeo(); attrs.put("X", pt.getX()); attrs.put("Y", pt.getY()); } Graphic gp = new Graphic(null, null, attrs,null); if(filePhotoName != null && !filePhotoName.equals("")) featureLayer.addAttachment(law.getObjectid(), new File(filePhotoName), new CallbackListener<FeatureEditResult>() { @Override public void onCallback(FeatureEditResult objs) { // TODO Auto-generated method stub Log.i("image", String.valueOf(objs.isSuccess())); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub } }); //featureLayer.addAttachment(law.getObjectid(), new File(filePhotoName), null); prog = ProgressDialog.show(LawEnforcementActivity.this, "提示信息", "数据正在提交中...."); prog.setCanceledOnTouchOutside(true); featureLayer.applyEdits(null, null, new Graphic[] { gp }, createEditCallbackListener(true)); } CallbackListener<FeatureEditResult[][]> createEditCallbackListener(final boolean updateLayer) { return new CallbackListener<FeatureEditResult[][]>() { public void onCallback(FeatureEditResult[][] result) { if (updateLayer) { featureLayer.refresh(); LawEnforcementActivity.this.runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub prog.dismiss(); Toast.makeText(LawEnforcementActivity.this, "数据提交成功", Toast.LENGTH_LONG).show(); finish(); } }); } } public void onError(Throwable e) { prog.dismiss(); } }; } public void enterpriseView(View v){ //featureQuery(FeatureUtils.ENTERPRISE_URL,MySQLiteHelper.ENTERPRISE_EXAMINE); MyQueryTask myquery = new MyQueryTask(); myquery.execute(); } private void featureQuery(String url,String tabletemp){ Query query = new Query(); query.setWhere("ENTERPRISE_ID = '"+law.getEnterpries_id()+"' order by objectid asc"); query.setOutFields(new String[] { "*" }); query.setReturnGeometry(false); //query.setInSpatialReference(temp.getSpatialReference()); QueryTask qTask = new QueryTask(url); try { FeatureSet queryResults = qTask.execute(query); Map<String, Object> attu = null; if(queryResults != null){ attu = queryResults.getGraphics()[0].getAttributes(); }else{ return ; } Intent intent = new Intent(); Enterprise enterprise = new Enterprise(); enterprise.setEnterprise_Name(attu.get("ENTERPRISE_NAME").toString()); if(attu.get("ENTERPRISE_ADDRESS") != null){ enterprise.setEnterprise_Address(attu.get("ENTERPRISE_ADDRESS").toString()); } if(attu.get("ENTERPRISE_CONTAMINATION")!=null){ enterprise.setEnterprise_contamination(attu.get("ENTERPRISE_CONTAMINATION").toString()); } if(attu.get("ENTERPRISE_ILLEGAL_RECORDS") != null){ enterprise.setEnterprise_illegal_records(attu.get("ENTERPRISE_ILLEGAL_RECORDS").toString()); } if(attu.get("ENTERPRISE_LAST_EXAMINE_TIMES") != null){ enterprise.setEnterprise_last_query_times(attu.get("ENTERPRISE_LAST_EXAMINE_TIMES").toString()); } if(attu.get("ENTERPRISE_LEGAL_PERSON") != null){ enterprise.setEnterprise_legal_person(attu.get("ENTERPRISE_LEGAL_PERSON").toString()); } Bundle bd = new Bundle(); bd.putSerializable("enterprise", enterprise); intent.putExtras(bd); intent.setClass(LawEnforcementActivity.this, EnterpriseActivity.class); startActivityForResult(intent, 1002); } catch (Exception e1) { // TODO Auto-generated catch block //e1.printStackTrace(); Toast.makeText(LawEnforcementActivity.this, "查询时出现问题,查看网络是否连接正常", Toast.LENGTH_LONG); } } class MyQueryTask extends AsyncTask<String, Void, FeatureSet>{ @Override protected FeatureSet doInBackground(String... params) { // TODO Auto-generated method stub featureQuery(FeatureUtils.ENTERPRISE_URL,MySQLiteHelper.ENTERPRISE_EXAMINE); return null; } @Override protected void onPostExecute(FeatureSet result) { // TODO Auto-generated method stub prog.dismiss(); } @Override protected void onPreExecute() { // TODO Auto-generated method stube prog = ProgressDialog.show(LawEnforcementActivity.this, "提示信息", "正在查询...."); } } }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值