zoukankan      html  css  js  c++  java
  • 调用相机实现拍照

    1.开启拍照权限

    2.xml文件

    <LinearLayout
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_marginTop="15dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:visibility="gone">

    <ImageView
    android:id="@+id/iv_num"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:src="@mipmap/contact9" />

    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/iv_num"
    android:layout_marginLeft="10dp"
    android:background="@null"
    android:focusable="true"
    android:hint="上传您的身份证正反面" />
    </LinearLayout>

    <LinearLayout
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="horizontal">

    <ImageView
    android:id="@+id/id1"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="100dp"
    android:clickable="true"
    android:scaleType="fitXY"
    android:layout_margin="5dp"
    android:background="@mipmap/contact_pic1" />

    <ImageView
    android:id="@+id/id2"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="100dp"
    android:clickable="true"
    android:scaleType="fitXY"
    android:layout_margin="5dp"
    android:background="@mipmap/contact_pic2" />

    </LinearLayout>

    </LinearLayout>
    3.MainActivity.class
    public class MainActivity extends Activity implements View.OnClickListener {
    /**
    * Called when the activity is first created.
    */
    private ImageView id1;
    private ImageView id2;
    private String SD_CARD_TEMP_DIR;
    private String SD_CARD_TEMP_DIR2;
    Bitmap myBitmap;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory()
    + File.separator + "front.jpg";//正面照片设定照相后保存的文件名,类似于缓存文件
    SD_CARD_TEMP_DIR2 = Environment.getExternalStorageDirectory()
    + File.separator + "reverse.jpg";//反面照片设定照相后保存的文件名,类似于缓存文件
    id1 = (ImageView) findViewById(R.id.id1);
    id2 = (ImageView) findViewById(R.id.id2);
    id1.setOnClickListener(this);
    id2.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.id1:
    //拍照
    setCameras(1);

    break;
    case R.id.id2:
    setCameras(-1);
    break;
    }
    }
    private void setCameras(int i) {
    Intent cameraIntent = new Intent(
    MediaStore.ACTION_IMAGE_CAPTURE);

    if (i == 1) {
    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
    Uri.fromFile(new File(SD_CARD_TEMP_DIR)));

    startActivityForResult(cameraIntent, 0);
    } else if (i == -1) {
    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
    Uri.fromFile(new File(SD_CARD_TEMP_DIR2)));

    startActivityForResult(cameraIntent, 1);
    }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0) {
    Log.e("requestCode", "Need 0");
    if (resultCode == RESULT_OK) {
    Log.e("resultCode", "OK!!!" + SD_CARD_TEMP_DIR);
    myBitmap = BitmapFactory.decodeFile(SD_CARD_TEMP_DIR);
    id1.setImageBitmap(myBitmap);
    } else {
    Log.e("resultCode", "" + resultCode);
    }
    } else if (requestCode == 1) {
    Log.e("requestCode", "Need 1");
    if (resultCode == RESULT_OK) {
    Log.e("resultCode", "OK!!!" + SD_CARD_TEMP_DIR2);
    myBitmap = BitmapFactory.decodeFile(SD_CARD_TEMP_DIR2);
    id2.setImageBitmap(myBitmap);
    } else {
    Log.e("resultCode", "" + resultCode);
    }
    } else {
    Log.e("requestCode", "Not Need");
    }
    }

    }
     
  • 相关阅读:
    Dom4j使用Xpath语法读取xml节点
    XML新手入门 创建构造良好的XML(2)
    XML新手入门 创建构造良好的XML(1)
    详解Java解析XML的四种方法
    Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .
    javaSE读取Properties文件的六种方法
    select
    socket
    socket异步通信-如何设置成非阻塞模式、非阻塞模式下判断connect成功(失败)、判断recv/recvfrom成功(失败)、判断send/sendto
    grep 多模式匹配
  • 原文地址:https://www.cnblogs.com/xiaoshumiao/p/9293119.html
Copyright © 2011-2022 走看看