Android realizes clicking on ImageView to take a picture and displaying the picture taken by the camera on the ImageView

First paste my registration page

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;

import android.graphics.Matrix;
import android.os.Bundle;

import android.provider.MediaStore;
import android.text.TextUtils;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import static com.example.myproject.R.id.roundRect3;

public class RegisterActivity extends Activity implements OnClickListener {
    private EditText eUsername;
    private EditText ePassword1;
    private EditText ePassword2;
    private Button submitBtn;
    private Button LoginButton;
    private ProgressDialog Progress;
    private ZQImageViewRoundOval iv_circle;
    private static int REQUEST_THUMBNAIL = 1;// Request thumbnail signal ID
    private float scaleWidth=1;
    private float scaleHeight=1;
    /*private static int REQUEST_ORIGINAL = 2;// Request the original image signal ID
    private String sdPath;//Path of SD card
    private String picPath;//Picture storage path*/

    public String serverUrl = "http://xxx.xx.xxx.x:8080/xxxxx/NewAccount";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_register);
        initViews();
    }

    private void initViews() {
        eUsername = (EditText) findViewById(R.id.new_username);
        ePassword1 = (EditText) findViewById(R.id.new_password_1);
        ePassword2 = (EditText) findViewById(R.id.new_password_2);

        submitBtn = (Button) findViewById(R.id.new_btn_submit);
        LoginButton = (Button) findViewById(R.id.btnLogin);

        submitBtn.setOnClickListener(this);
        LoginButton.setOnClickListener(this);
        iv_circle=(ZQImageViewRoundOval)findViewById(roundRect3);
        iv_circle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.new_btn_submit:
                TextToString();
                break;
            case R.id.btnLogin:
                ToLogin();
                break;
            case roundRect3:
                OpenCamera();
                break;
            default:
                break;
        }

    }
    private void  OpenCamera(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);
    }
    /**
     * Callback method when returning to the application
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_THUMBNAIL) {//Corresponding to the first method
                /**
                 * The shots taken out by this method will be compressed by default, because if the pixel of the camera is relatively high, the pictures taken will be relatively high-definition,
                 * If the image is too large, it will cause memory overflow (OOM), so this method will compress the image by default
                 */
                Bundle bundle = data.getExtras();
                Bitmap bmp = (Bitmap) bundle.get("data");
                int bmpWidth = bmp.getWidth();
                int bmpHeight = bmp.getHeight();
                if(bmpWidth<=180&&bmpWidth<=180){
                    ((ImageView) findViewById(R.id.roundRect3)).setImageBitmap(bmp);// Display the picture in the ImageView
                }else {
                /* Set the zoom ratio of the image */
                    double scale = 4;

                /* Calculate the scale to zoom in this time */
                    scaleWidth = (float) (scaleWidth * scale);

                    scaleHeight = (float) (scaleHeight * scale);

                /* Generate Bitmap object after reSize */
                    Matrix matrix
                            = new Matrix();
                    matrix.postScale(scaleWidth, scaleHeight);
                    Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
                    ((ImageView) findViewById(R.id.roundRect3)).setImageBitmap(resizeBmp);// Display the picture in ImageView
                }
            }
        }
    }
    private void TextToString() {
        String username = eUsername.getText().toString();
        String password = ePassword1.getText().toString();
        CheckTextBox();
        Register(username, password);
    }

    private void Register(final String username, final String password) {
        Progress = new ProgressDialog(this);
        Progress.setCancelable(false);
        Progress.setCanceledOnTouchOutside(false);
        ADataBase db = new ADataBase();
        int jsonResult = db.WebConnoct(username, password, serverUrl);
        hanleCreateAccountResult(jsonResult);
    }

    private void ToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    public void hanleCreateAccountResult(int jsonResult) {
        /*
         *   result_code:
         * 0 successfully registered
         * 1 Username already exists
         * 2 Abnormal database operation
         * */
        int result;
        result = jsonResult;

        if(result == 1) {
            Toast.makeText(this, "Username already exists!", Toast.LENGTH_LONG).show();
            return;
        }

        if(result == 2) {
            Toast.makeText(this, "Registration failed! An exception occurred on the server side!", Toast.LENGTH_LONG).show();
            return;
        }

        if(result == 0) {
            Progress.dismiss();
            Toast.makeText(this, "Successful registration! Go to the login page!", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
            return;
        }
    }

    private boolean checkUsername() {
        String username = eUsername.getText().toString();
        if(TextUtils.isEmpty(username)) {
            return false;
        }
        return true;
    }

    private int checkPassword() {
        /*
         * return value:
         * 0 password valid
         * 1 password not equal 2 inputs
         * 2 password empty
         * */
        String pwd1 = ePassword1.getText().toString();
        String pwd2 = ePassword2.getText().toString();
        if(!pwd1.equals(pwd2)) {
            return 1;
        } else if(TextUtils.isEmpty(pwd1)) {
            return 2;
        } else {
            return 0;
        }
    }

    private void CheckTextBox() {
        boolean isUsernameValid = checkUsername();
        if(!isUsernameValid) {
            Toast.makeText(this, "The username is incorrect, please re-enter", Toast.LENGTH_LONG).show();
            return;
        }

        int pwdResult = checkPassword();
        if(pwdResult == 1) {
            Toast.makeText(this, "The passwords entered twice are inconsistent, please confirm!", Toast.LENGTH_LONG).show();
            return;
        }
        if (pwdResult == 2) {
            Toast.makeText(this, "Password cannot be empty!", Toast.LENGTH_LONG).show();
            return;
        }
    }

}

Registered xml page code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/bg2"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myproject.RegisterActivity" >

    <EditText
        android:id="@+id/new_password_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/txtWrite"
        android:layout_alignLeft="@+id/new_username"
        android:ems="10"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/new_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/new_password_1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="15dp"
        android:ems="10" />

    <com.example.myproject.AImageViewRoundOval
        android:id="@+id/roundRect3"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:src="@mipmap/userpic"
        android:layout_above="@+id/btnLogin"
        android:layout_alignTop="@+id/new_username">
    </com.example.myproject.AImageViewRoundOval>
    <TextView
        android:id="@+id/txtSound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/new_password_1"
        android:layout_alignLeft="@+id/txtWrite"
        android:text="Username:"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/new_password_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/new_password_1"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textPassword" />

    <TextView
        android:id="@+id/txtWrite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/new_password_2"
        android:layout_alignLeft="@+id/TextView01"
        android:text="password:"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/new_password_2"
        android:layout_toLeftOf="@+id/new_password_2"
        android:text="Confirm password:"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/new_password_2"
        android:layout_toRightOf="@+id/txtWrite"
        android:text="I have read and accept the User Agreement"
        android:textSize="14sp" />

    <Button
        android:id="@+id/new_btn_submit"
        android:layout_width="100dp"
        android:layout_height="38dp"
        android:layout_below="@+id/checkBox1"
        android:layout_centerHorizontal="true"
        android:text="Submit registration"
        android:textSize="12sp" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="100dp"
        android:layout_height="38dp"
        android:layout_alignBaseline="@+id/new_btn_submit"
        android:layout_alignBottom="@+id/new_btn_submit"
        android:layout_alignRight="@+id/new_password_2"
        android:text="Click to log in"
        android:textSize="12sp" />

</RelativeLayout>

The following are instructions:

Pasting the above code directly will cause problems, because of the lack of a custom class - AImageViewRoundOval.java, the code I have already put in the previous article. https://my.oschina.net/u/3724795/blog/1583883

 

Let's talk about the onActivityResult method. The bundle method is used here to compress the captured image, but I found at runtime that if the following code is used directly, the image will be reduced and ugly.

 /**
     * 返回应用时回调方法
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_THUMBNAIL) {//对应第一种方法
                /**
                 * 通过这种方法取出的拍摄会默认压缩,因为如果相机的像素比较高,拍摄出来的图会比较高清,
                 * 如果图太大会造成内存溢出(OOM),因此此种方法会默认给图片尽心压缩
                 */
                Bundle bundle = data.getExtras();
                Bitmap bmp = (Bitmap) bundle.get("data");
               ((ImageView) findViewById(R.id.roundRect3)).setImageBitmap(bmp);// 将图片显示在ImageView里
        
        }
    }

So I redefine the size of the ImageView and enlarge the image to the original size (I also found that the loophole in this code is that if the camera is using proactive, even if I redefine the size, it is useless, it is still so small ), in fact, the magnification ratio can be adjusted according to your own needs. In fact, if you master this code, you can make a form that zooms in and out of the picture.

/* 设置图片放大的比例 */
                    double scale = 4;

                /* 计算这次要放大的比例 */
                    scaleWidth = (float) (scaleWidth * scale);

                    scaleHeight = (float) (scaleHeight * scale);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325982146&siteId=291194637