如何选择一张图像来设置头像

发布于 2024-12-11 01:13:02 字数 891 浏览 0 评论 0原文

我有一个应用程序,用户可以通过给自己拍照或从图库中选择一张照片来设置自己的头像。我在其他应用程序中看到,用户在选择图像后会显示一个视图,用户可以在其中“绘制”一个矩形,选择他想要用作头像的图像区域。 我想在我的应用程序中包含这种可能性。拍完照片后怎么办?

谢谢!

编辑:

我试图用这个来做到这一点,但它打开的是图片库而不是相机:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.setType("image/*");
            cameraIntent.putExtra("crop", "true");
            cameraIntent.putExtra("scale", "true");
            cameraIntent.putExtra("outputX", 100);
            cameraIntent.putExtra("outputY", 100);
            cameraIntent.putExtra("aspectX", 1);
            cameraIntent.putExtra("aspectY", 1);
            cameraIntent.putExtra("max-width", 30);
            cameraIntent.putExtra("max-height", 30);
            cameraIntent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(cameraIntent, IMAGEN_CAMARA);    

I've an application where user can set his own avatar by taking himself a picture or just picking a picture from the gallery. I've seen in other apps that user, after pick an image its displayed a view where user can "draw" a rectangle selecting which area of the image he wants to use as avatar.
I would like to include this possibility in my app. How can I do that after taking the picture?

Thanks!

Edit:

I'm trying to do it with this but it opens image gallery instead of camera:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.setType("image/*");
            cameraIntent.putExtra("crop", "true");
            cameraIntent.putExtra("scale", "true");
            cameraIntent.putExtra("outputX", 100);
            cameraIntent.putExtra("outputY", 100);
            cameraIntent.putExtra("aspectX", 1);
            cameraIntent.putExtra("aspectY", 1);
            cameraIntent.putExtra("max-width", 30);
            cameraIntent.putExtra("max-height", 30);
            cameraIntent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(cameraIntent, IMAGEN_CAMARA);    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-12-18 01:13:02

这就是我在使用此功能的一个应用程序中实现它的方式。这很简单。

private void doTakePhotoAction() {
// http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo
// http://stackoverflow.com/questions/1050297/how-to-get-the-url-of-the-captured-image
// http://www.damonkohler.com/2009/02/android-recipes.html
// http://www.firstclown.us/tag/android/
// The one I used to get everything working: http://groups.google.com/group/android-developers/msg/2ab62c12ee99ba30 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

//Wysie_Soh: Create path for temp file
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                    "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

try {
    intent.putExtra("return-data", true);
    startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
    //Do nothing for now
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
    return;
}

switch (requestCode) {

case CROP_FROM_CAMERA: {
    //Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here
    //after the image is cropped.

    final Bundle extras = data.getExtras();

    if (extras != null) {
        Bitmap photo = extras.getParcelable("data");

        mPhoto = photo;
        mPhotoChanged = true;
        mPhotoImageView.setImageBitmap(photo);
        setPhotoPresent(true);
    }

    //Wysie_Soh: Delete the temporary file                        
    File f = new File(mImageCaptureUri.getPath());            
    if (f.exists()) {
        f.delete();
    }

    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);

    break;
}

case PICK_FROM_CAMERA: {
    //After an image is taken and saved to the location of mImageCaptureUri, come here
    //and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setClassName("com.android.camera", "com.android.camera.CropImage");

    intent.setData(mImageCaptureUri);
    intent.putExtra("outputX", 96);
    intent.putExtra("outputY", 96);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);            
    startActivityForResult(intent, CROP_FROM_CAMERA);

    break;

}
}

希望

它有帮助:)

This is how i implemented it in one of my apps that uses this feature. It is quite simple.

private void doTakePhotoAction() {
// http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo
// http://stackoverflow.com/questions/1050297/how-to-get-the-url-of-the-captured-image
// http://www.damonkohler.com/2009/02/android-recipes.html
// http://www.firstclown.us/tag/android/
// The one I used to get everything working: http://groups.google.com/group/android-developers/msg/2ab62c12ee99ba30 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

//Wysie_Soh: Create path for temp file
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                    "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

try {
    intent.putExtra("return-data", true);
    startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
    //Do nothing for now
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
    return;
}

switch (requestCode) {

case CROP_FROM_CAMERA: {
    //Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here
    //after the image is cropped.

    final Bundle extras = data.getExtras();

    if (extras != null) {
        Bitmap photo = extras.getParcelable("data");

        mPhoto = photo;
        mPhotoChanged = true;
        mPhotoImageView.setImageBitmap(photo);
        setPhotoPresent(true);
    }

    //Wysie_Soh: Delete the temporary file                        
    File f = new File(mImageCaptureUri.getPath());            
    if (f.exists()) {
        f.delete();
    }

    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);

    break;
}

case PICK_FROM_CAMERA: {
    //After an image is taken and saved to the location of mImageCaptureUri, come here
    //and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setClassName("com.android.camera", "com.android.camera.CropImage");

    intent.setData(mImageCaptureUri);
    intent.putExtra("outputX", 96);
    intent.putExtra("outputY", 96);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);            
    startActivityForResult(intent, CROP_FROM_CAMERA);

    break;

}
}

}

Hope it helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文