build.gradle文件库的引用:
implementation 'com.google.zxing:core:3.2.1'
识别条形码的activity:
public void onScanBarcode(View v){ IntentIntegrator integrator = new IntentIntegrator(this); integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES); integrator.setPrompt("扫描条形码"); integrator.setCameraId(0); integrator.setBeepEnabled(false); integrator.initiateScan(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if(result != null) { if(result.getContents() == null) { Toast.makeText(this, "扫码取消!", Toast.LENGTH_LONG).show(); } else { final String stuno = result.getContents(); new Thread(){ @Override public void run() { getUserName(stuno); } }.start(); Toast.makeText(this, "扫描成功,条码值:" + result.getContents(), Toast.LENGTH_LONG).show(); } } else { super.onActivityResult(requestCode, resultCode, data); } }
result.getContents()得到的是一个数字,即扫描条形码得出的结果
拍照的activity
import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.MediaStore; import android.support.v4.content.FileProvider; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.example.ceo.identifyapp.util.UpLoadFileToSerUtil; import com.example.ceo.identifyapp.util.UrlUtil; import java.io.File; import java.io.IOException; public class GetPictureActivity extends AppCompatActivity { private final int TAKE_PHOTO = 1;//拍照操作 /* * 拍照所得到的图像的保存路径 */ private Uri imageUri; private String image; private int i=0; //相机请求码 private static final int REQUESTCODE_CAMERA = 1002; private ImageView picture1; private ImageView picture2; private ImageView picture3; private Button takephoto; private Button complete; int j=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_picture); complete=(Button)findViewById(R.id.BUTTON_COMPLETE); takephoto=(Button)findViewById(R.id.BUTTON_TAKE_PHOTO); picture1=(ImageView)findViewById(R.id.picture1); picture2=(ImageView)findViewById(R.id.picture2); picture3=(ImageView)findViewById(R.id.picture3); complete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(); intent.setClass(GetPictureActivity.this,MainActivity.class); startActivity(intent); finish(); } }); takephoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { takePhoto(); } }); } public void takePhoto(){ /* * 当前用户拍照或者从相册选择的照片的文件名 */ String stuname= getIntent().getStringExtra("stuname"); String stuid= getIntent().getStringExtra("stuid"); String fileName=stuid+"-"+stuname; File outputImage; if (new File(UrlUtil.IMAGES_UPLOAD+fileName+"-"+(i+1)+".png").exists()){ while (new File(UrlUtil.IMAGES_UPLOAD+fileName+"-"+(i+1)+".png").exists()){ i++; } outputImage=new File(UrlUtil.IMAGES_UPLOAD+fileName+"-"+(i+1)+".png"); try { outputImage.createNewFile(); }catch (IOException e){ e.printStackTrace(); } Uri contentUri = FileProvider.getUriForFile(this, "com.example.ceo.identifyapp.fileprovider", outputImage); image=outputImage.toString(); /* * 启动系统的照相Intent */ Intent intent=new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT,contentUri); startActivityForResult(intent,TAKE_PHOTO); }else { outputImage=new File(UrlUtil.IMAGES_UPLOAD+fileName+"-"+(i+1)+".png");//照片名字取名为特定人名字加第几张 try { outputImage.createNewFile(); }catch (IOException e){ e.printStackTrace(); } Uri contentUri = FileProvider.getUriForFile(this, "com.example.ceo.identifyapp.fileprovider", outputImage);//因为新版本的安卓需要靠fileprovider去临时赋予权限,不然会报错 image=outputImage.toString(); Intent intent=new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT,contentUri); startActivityForResult(intent,TAKE_PHOTO); } } /** * 因为启动拍照intent的时候是使用的forResult模式,因此需要onActivityResult方法来接受回调参数 * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); String stuname= getIntent().getStringExtra("stuname"); String stuid= getIntent().getStringExtra("stuid"); String fileName=stuid+"-"+stuname; if (resultCode != RESULT_OK) { Toast.makeText(this, "获取图片出现错误", Toast.LENGTH_SHORT).show(); } else{ switch(requestCode) { /* * case TAKE_PHOTO 代表从拍摄照片的intent返回之后 * 完成拍摄照片之后,立刻打开系统自带的裁剪图片的intent */ case TAKE_PHOTO: try { new Thread(){ @Override public void run() { UpLoadFileToSerUtil.uploadFile(new File(image),UrlUtil.IMAGES_UPLOAD_URL); } }.start(); }catch (Exception e){ e.printStackTrace(); } Bitmap bitmap = BitmapFactory.decodeFile(image); if (j==1){picture1.setImageBitmap(bitmap); }else if (j==2){ picture2.setImageBitmap(bitmap); }else if (j==3){ picture3.setImageBitmap(bitmap); break;//第三张照片的时候不再拍照 } takephoto.setVisibility(View.GONE); j++; takePhoto(); Toast.makeText(this,"图片保存成功!", Toast.LENGTH_SHORT).show(); default: break; } } } }