簡體   English   中英

裁剪圖像並將其保存到mysqldatabse中

[英]cropped image and save it into mysqldatabse

我既可以捕獲圖像,也可以裁剪圖像。但是,當我上傳圖像時,我無法保存裁剪的圖像。與其上傳完整的圖像,不如只上傳裁剪的圖像。 。

public class imageuploadtest extends AppCompatActivity implements View.OnClickListener {
private ImageView Imageview;
EditText Name;
private Button Upload, Capture,camera;
private final int IMG_REQUEST = 1;
public Bitmap bitmap;
ImageView imageView;
Button buttonCamera, buttonGallery, submit;
File file;
Uri uri;
EditText date;
Intent CamIntent, GalIntent, CropIntent;
public static final int RequestPermissionCode = 1;
DisplayMetrics displayMetrics;
int width, height;
String server_url = "http://10.0.0.01/adcube/imageupload.php";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imageupload);
    Imageview = (ImageView) findViewById(R.id.imageView);
    Upload = (Button) findViewById(R.id.upload);
    Capture = (Button) findViewById(R.id.capture);
    Name = (EditText) findViewById(R.id.name);
    camera= (Button) findViewById(R.id.camera);
    Upload.setOnClickListener(this);
    Capture.setOnClickListener(this);
    camera.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.upload:
            selectimage();
            break;
        case R.id.capture:

            uploadImage();
            break;
        case R.id.camera:
            ClickImageFromCamera();
    }
}

public void ClickImageFromCamera() {

    CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    file = new File(Environment.getExternalStorageDirectory(),
            "file" + String.valueOf(System.currentTimeMillis()) + ".jpg");
    uri = Uri.fromFile(file);

    CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);

    CamIntent.putExtra("return-data", true);

    startActivityForResult(CamIntent, 0);

}
private void selectimage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, IMG_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0 && resultCode == RESULT_OK) {
        ImageCropFunction();

    } else if (requestCode == 2) {

        if (data != null) {

            uri = data.getData();

            ImageCropFunction();
        }
    } else if (requestCode == 1) {

        if (data != null) {

ImageCropFunction();
                try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                Bundle bundle = data.getExtras();

                Bitmap bitmap =bundle.getParcelable("data");
                Imageview.setImageBitmap(bitmap);
                Imageview.setVisibility(View.VISIBLE);
                Name.setVisibility(View.VISIBLE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}



private void uploadImage() {
    StringRequest stringrequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String Response = jsonObject.getString("cropIntent");
                Toast.makeText(imageuploadtest.this, Response, Toast.LENGTH_SHORT).show();
                Imageview.setImageResource(0);
                Imageview.setVisibility(View.GONE);
                Name.setText("");
                Name.setVisibility(View.GONE);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Toast.makeText(imageuploadtest.this, "successful", Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(imageuploadtest.this, error.toString(), Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> parms = new HashMap<>();
            parms.put("name", Name.getText().toString().trim());
            parms.put("image", imageToString(bitmap));

            return parms;
        }
    };
    Mysingleton.getInstance(imageuploadtest.this).addToRequest(stringrequest);
}
public void ImageCropFunction() {

    // Image Crop Code
    try {
        CropIntent = new Intent("com.android.camera.action.CROP");

        CropIntent.setDataAndType(uri, "image/*");

        CropIntent.putExtra("crop", "true");
        CropIntent.putExtra("outputX", 180);
        CropIntent.putExtra("outputY", 180);
        CropIntent.putExtra("aspectX", 3);
        CropIntent.putExtra("aspectY", 4);

        CropIntent.putExtra("scaleUpIfNeeded", true);
        CropIntent.putExtra("return-data", true);

        startActivityForResult(CropIntent, 1);

    } catch (ActivityNotFoundException e) {

    }
}


//cconvert image to string for database

public static String imageToString(Bitmap BitmapData) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BitmapData.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] byte_arr = bos.toByteArray();
    String encoded = Base64.encodeToString(byte_arr, Base64.DEFAULT); //appendLog(file);
    return encoded;
  }
}

這個onActivityResult應該是這樣的

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
    ImageCropFunction();

} else if (requestCode == 2) {

    if (data != null) {

        uri = data.getData();

        ImageCropFunction();
    }
} else if (requestCode == 1) {

    if (data != null) {
ImageCropFunction(); 
try {

            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            Bundle bundle = data.getExtras();

            bitmap =bundle.getParcelable("data");
            Imageview.setImageBitmap(bitmap);
            Imageview.setVisibility(View.VISIBLE);
            Name.setVisibility(View.VISIBLE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我剛剛刪除了一個額外的位圖

Bitmap bitmap =bundle.getParcelable("data");

應該只這樣

bitmap =bundle.getParcelable("data");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM