修改Android中照相机拍摄的图像大小

修改Android中照相机拍摄的图像大小,android,android-camera,Android,Android Camera,我正在尝试拍摄指定尺寸的图像,并将其保存在SD卡上所需的位置。我正在使用intent.putExtra通过默认相机应用程序拍摄图像 代码在这里 public void onClick(View v) { //Setting up the URI for the desired location imageFile = "bmp"+v.getId()+".png"; File f = new File (folder,imageFile); imageUri = U

我正在尝试拍摄指定尺寸的图像,并将其保存在SD卡上所需的位置。我正在使用intent.putExtra通过默认相机应用程序拍摄图像

代码在这里

public void onClick(View v) {
    //Setting up the URI for the desired location
    imageFile = "bmp"+v.getId()+".png";
    File f = new File (folder,imageFile);
    imageUri = Uri.fromFile(f);

    //Setting the desired size parameters
    private Camera mCamera;      
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewSize(width, height);
    mCamera.setParameters(parameters);       

    //Passing intent.PutExtras to defaul camera activity
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(i,CAMERA_PIC_REQUEST);   
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
    return;
}
拍摄图像后,相机活动力关闭。 是否可以通过这种方式修改默认照相机活动拍摄的图像的大小


或者需要单独的摄像头应用程序???

如果图像保存为文件,请从该文件创建位图,并使用此方法减小其大小,然后将此位图传递给活动:

public static Bitmap decodeFile(File file, int requiredSize) {
    try {

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file), null, o);

        // The new size we want to scale to

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < requiredSize
                    || height_tmp / 2 < requiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(file), null,
                o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}
公共静态位图解码文件(文件文件,int requiredSize){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
decodeStream(新文件输入流(文件),null,o);
//我们要扩展到的新尺寸
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<所需尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(新文件InputStream(文件)),null,
氧气);
}catch(filenotfounde异常){
}
返回null;
}

如果您不告诉我们问题是什么,我们无法理解为什么会出现问题。我已经编辑了问题,请看一看,您能用force close的完整日志编辑您的问题吗?而且,如果您能够阅读它们,突出显示该部分代码也会很有用。问题是由于某些原因,我无法使用USB调试,因此无法访问logcat。然后您必须逐行注释以查找问题,或者找到USB调试的方法。(真的不应该有任何理由不能将手机插入机器。)