如何在存储 SD 卡之前裁剪捕获的图像? [复制]
Posted
技术标签:
【中文标题】如何在存储 SD 卡之前裁剪捕获的图像? [复制]【英文标题】:How to crop captured image before storing sd card? [duplicate] 【发布时间】:2013-06-03 05:50:24 【问题描述】:嗨,我是 android 的新人....我想在捕获图像时裁剪并将图像保存在 sd 卡中....当我们捕获图像时它不存储在 sd 卡中...之后只裁剪它保存在sd卡中?...图像质量不应该影响...我们可以知道这条路径..我没有关于这个的代码...如果可以请提供完整的代码..这个代码被强制关闭..我不知道如何解决这个.. 请提前告诉我谢谢
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;
public class ImageResize
private Context mContext;
private int mWidth;
private int mHeight;
private Uri mImageUri;
private BitmapFactory.Options mBitMapOptions;
private Bitmap mBitMap;
private Bitmap tempBitMap;
public ImageResize(Context context, int width, int height, Uri imgUri)
this.mContext = context;
this.mWidth = width;
this.mHeight = height;
this.mImageUri = imgUri;
public Bitmap getResizeImage()
ContentResolver resolver = mContext.getContentResolver();
mBitMapOptions = new BitmapFactory.Options();
if(mImageUri != null)
ParcelFileDescriptor fd = null;
try
fd = resolver.openFileDescriptor(mImageUri, "r");
int sampleSize = 1;
mBitMapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, mBitMapOptions);
int nextWidth = mBitMapOptions.outWidth >> 1;
int nextHeight = mBitMapOptions.outHeight >> 1;
while(nextWidth > mWidth && nextHeight > mHeight)
sampleSize <<= 1;
nextWidth >>= 1;
nextHeight >>= 1;
mBitMapOptions.inSampleSize = sampleSize;
mBitMapOptions.inJustDecodeBounds = false;
mBitMap = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, mBitMapOptions);
Log.d("Result","Image use Size : " +mWidth+"," +mHeight);
Log.d("Result","Image Size : " +mBitMap.getWidth()+"," + mBitMap.getHeight());
Log.d("Result","aa : " +(mWidth*mBitMap.getHeight())/mBitMap.getWidth());
if(mBitMap!=null)
if(mBitMapOptions.outWidth != mWidth || mBitMapOptions.outHeight != mHeight)
//??????? : ???? ???????? = (???? ???????? * ?????????) / ?????????
tempBitMap = Bitmap.createScaledBitmap(mBitMap, mWidth, (mWidth*mBitMap.getHeight())/mBitMap.getWidth(), true);
mBitMap.recycle();
mBitMap = tempBitMap;
return mBitMap;
catch (FileNotFoundException e)
Log.e(getClass().getSimpleName(), e.getMessage(), e);
finally
try if(fd != null) fd.close(); catch (IOException e) Log.e(getClass().getSimpleName(), e.getMessage(), e);
if(mBitMap != null) mBitMap = null;
if(tempBitMap != null) tempBitMap = null;
return null;
【问题讨论】:
【参考方案1】:如果您已经有了位图,下面的代码将对其进行裁剪并将其保存在指定的路径中(确保路径存在)。该文件将以指定的格式保存。如果您选择 jpeg,则必须指定品质因数。保存为 png 将忽略质量参数。
File outputFile = new File("/sdcard/SaveDir/SaveBitmap.png");
//x,y is the starting point and width,height is the distance from start
//createBitmap(Bitmap source, int x, int y, int width, int height)
Bitmap bitmap = Bitmap.createBitmap(originalBitmap, 0, 0, 20, 20);
OutputStream fout = null;
try
fout = new FileOutputStream(outputFile);
//if format is png(lossless) then quality argument is ignored
//(Bitmap.CompressFormat format, int quality, OutputStream stream)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
fout.flush();
fout.close();
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
【讨论】:
以上是关于如何在存储 SD 卡之前裁剪捕获的图像? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
Android - 如何使用新的存储访问框架将文件复制到外部 sd 卡