java - 拍摄图像并裁剪

标签 java android

我正在学习本教程:http://mobile.tutsplus.com/tutorials/android/capture-and-crop-an-image-with-the-device-camera/

我正在尝试创建一个简单的 Activity ,它有一个“take pictureButton 和一个 ImageView,只需拍一张照片,然后打开 Android 内置的裁剪 Activity 。我可以毫无意外地打开相机,但是,在拍照时,代码不会将照片发送到裁剪 Activity 。

调用裁剪 Activity 时似乎崩溃了。我不确定为什么会这样;我完全按照这个例子(除了我不需要的开始的 XML 内容),我查看了代码,一切似乎都有意义。我确定这是导致此问题的某个地方的小错误。这是我的 Activity 代码:

package com.example.project;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageChoose extends Activity implements OnClickListener {

//keep track of camera capture intent
final int CAMERA_CAPTURE = 1;
//captured picture uri
private Uri picUri;
final int PIC_CROP = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_choose);
    Button takePicture = (Button)findViewById(R.id.takePicture);
    takePicture.setOnClickListener(this);
}

public void onClick(View v) {
    if (v.getId() == R.id.takePicture){
        try{
            //use standard intent to capture an image
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //we will handle the returned data in onActivityResult
            startActivityForResult(captureIntent, CAMERA_CAPTURE);
        }catch(ActivityNotFoundException anfe){
            //display an error message
            String errorMessage = "Your device doesn't support photos!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (resultCode == RESULT_OK){
        if (requestCode == CAMERA_CAPTURE){
            picUri = data.getData();
            performCrop();
        }else if(requestCode == PIC_CROP){
            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            //retrieve a reference to the ImageView
            ImageView picView = (ImageView)findViewById(R.id.picture);
            //display the returned cropped image
            picView.setImageBitmap(thePic);
        }
    }
}

private void performCrop(){
    try{
        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
            //indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
            //set crop properties
        cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
            //indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
            //retrieve data on return
        cropIntent.putExtra("return-data", true);
            //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);
    }catch(ActivityNotFoundException anfe){
        String errorMessage = "Your device doesn't support photo cropping!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
  }

  }

最佳答案

我已经使用了这种类型的操作。这是我的代码和以下链接:- Detail Description

希望这对您有所帮助。我建议您关注以下几行:-

Intent camera=new Intent();
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra("crop", "true");

关于java - 拍摄图像并裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13561026/

相关文章:

java - 自动 Python 到 Java 翻译

android - 在保持原始大小写的同时突出显示 ListView 中的搜索词

android - 当应用程序处于后台时, Activity 和 fragment 会发生什么

android - SQLite 数据库仅存储通过 ContentProvider 发送的最后一条记录

android - 内存高效地下载和解析 JSON

android - 将 View 及其父 View 的 onTouchListeners 分开

java - 跳棋游戏板 : store object refs or char values?

java - istream.write 之后的套接字 DataInputStream 和 EOF 标志 : Is there a "conflict" when writing with istream. writeUTF?

java - 使用 JNI 生成 c/c++ header

java - Java多个文件和文件夹层次结构