簡體   English   中英

我如何裁剪圖像並將其存儲在gridview中?

[英]how can i crop the image and store it in gridview?

我正在構建一個應用程序,其中用戶可以從相機捕獲圖像,然后編輯該圖像並在gridview中存儲/顯示該圖像。我面臨的問題是,當用戶捕獲圖像而不是裁剪圖像時,它將直接顯示它在gridview中。 我需要幫助,在此先感謝,這是我的代碼

public class MainActivity extends Activity implements OnClickListener {

Button captureBtn = null;
final int CAMERA_CAPTURE = 1;
private Uri picUri;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private GridView grid;
private  List<String> listOfImagesPath;
Intent CropIntent;
public static final String GridViewDemo_ImagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/GridViewDemo/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    captureBtn = (Button)findViewById(R.id.capture_btn1);
    captureBtn.setOnClickListener(this);
    grid = ( GridView) findViewById(R.id.gridviewimg);

    listOfImagesPath = null;
    listOfImagesPath = RetriveCapturedImagePath();
    if(listOfImagesPath!=null){
        grid.setAdapter(new ImageListAdapter(this,listOfImagesPath));
    }
}
public void ImageCropFunction() {

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

        CropIntent.setDataAndType(picUri, "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) {

    }
}
//Image Crop Code End Here


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
    if (arg0.getId() == R.id.capture_btn1) {

        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 = "Whoops - your device doesn't support 
capturing images!";
            Toast toast = Toast.makeText(this, errorMessage, 
Toast.LENGTH_SHORT);
            toast.show();
        }
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {

//user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){
            ImageCropFunction();
            Bundle extras = data.getExtras();
            Bitmap thePic = extras.getParcelable("data");
            String imgcurTime = dateFormat.format(new Date());
            File imageDirectory = new File(GridViewDemo_ImagePath);
            imageDirectory.mkdirs();
            String _path = GridViewDemo_ImagePath + imgcurTime+".jpg";
            try {
                FileOutputStream out = new FileOutputStream(_path);
                thePic.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.close();
            } catch (FileNotFoundException e) {
                e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
            }
            listOfImagesPath = null;
            listOfImagesPath = RetriveCapturedImagePath();
            if(listOfImagesPath!=null){
                grid.setAdapter(new ImageListAdapter(this,listOfImagesPath));
            }

        }
    }
}

private List<String> RetriveCapturedImagePath() {
    List<String> tFileList = new ArrayList<String>();
    File f = new File(GridViewDemo_ImagePath);
    if (f.exists()) {
        File[] files=f.listFiles();
        Arrays.sort(files);

        for(int i=0; i<files.length; i++){
            File file = files[i];
            if(file.isDirectory())
                continue;
            tFileList.add(file.getPath());
        }
    }
    return tFileList;
}

public class ImageListAdapter extends BaseAdapter
{
    private Context context;
    private List<String> imgPic;
    public ImageListAdapter(Context c, List<String> thePic)
    {
        context = c;
        imgPic = thePic;
    }
    public int getCount() {
        if(imgPic != null)
            return imgPic.size();
        else
            return 0;
    }

    //---returns the ID of an item---
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ImageView imageView;
        BitmapFactory.Options bfOptions=new BitmapFactory.Options();
        bfOptions.inDither=false;                     //Disable Dithering mode
        bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        bfOptions.inTempStorage=new byte[32 * 1024];
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }
        FileInputStream fs = null;
        Bitmap bm;
        try {
            fs = new FileInputStream(new File(imgPic.get(position).toString()));

            if(fs!=null) {
                bm=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
                imageView.setImageBitmap(bm);
                imageView.setId(position);
                imageView.setLayoutParams(new GridView.LayoutParams(200, 160));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(fs!=null) {
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return imageView;
    }
}
}

讓我知道是否有效

首先改變

startActivityForResult(CropIntent, 1); 

`startActivityForResult(CropIntent, 2);`

並將其添加到onActivityResult中

picUri = Uri.parse(_path);

之后,在您的onActivityResult中刪除

listOfImagesPath = null;
            listOfImagesPath = RetriveCapturedImagePath();
            if(listOfImagesPath!=null){
                grid.setAdapter(new ImageListAdapter(this,listOfImagesPath));
            }

並添加新的結果,例如

if(requestCode == CAMERA_CAPTURE){
....
} else if(requestCode == 2){
..
 }

在新語句的最后一步,添加以下行:

else if(requestCode == 2){
listOfImagesPath = null;
            listOfImagesPath = RetriveCapturedImagePath();
            if(listOfImagesPath!=null){
                grid.setAdapter(new ImageListAdapter(this,listOfImagesPath));
            }
}

暫無
暫無

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

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