设置相机拍摄照片的大小

时间:2011-04-27 07:02:39

标签: android

有没有办法不断设置我自己的相机应用程序拍摄的照片分辨率(如256x256)。这是一些代码,谢谢。

SurfaceView cameraView;
SurfaceHolder surfaceHolder;
Camera camera;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    File mFile=new File("/sdcard/Galmix");
    mFile.mkdir();

    cameraView = (SurfaceView)this.findViewById(R.id.CameraView);
    surfaceHolder = cameraView.getHolder();
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceHolder.addCallback(this);

    cameraView.setFocusable(true);
    cameraView.setFocusableInTouchMode(true);
    cameraView.setClickable(true);

    cameraView.setOnClickListener(this);
}
public void onClick(View v){
    camera.takePicture(null, null, this);
}

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    try{
        OutputStream imageFileOS = new FileOutputStream(String.format("/sdcard/Galmix/%d.jpg",System.currentTimeMillis()));
        imageFileOS.write(data);
        imageFileOS.flush();
        imageFileOS.close();
    } catch(FileNotFoundException e){
        Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
        t.show();
    } catch (IOException e) {
        Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
        t.show();
    }
    camera.startPreview();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
    camera.startPreview();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    try{
        camera.setPreviewDisplay(holder);
        Camera.Parameters parameters = camera.getParameters();

        if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
            parameters.set("orientation", "portrait");
            camera.setDisplayOrientation(90);   
        }
        List<String> colorEffects = parameters.getSupportedColorEffects();
        Iterator<String> cei = colorEffects.iterator();
        while(cei.hasNext()){
            String currentEffect = cei.next();
            if(currentEffect.equals(Camera.Parameters.EFFECT_SOLARIZE)){
                parameters.setColorEffect(Camera.Parameters.EFFECT_SOLARIZE); break;
            }
        }
        camera.setParameters(parameters);
    } catch(IOException e){
        camera.release();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
}

2 个答案:

答案 0 :(得分:1)

有关信息,请参阅Camera Parameters

答案 1 :(得分:1)

要解决您的问题,您必须使用以下代码检查相机支持的尺寸:

Camera camera = Camera.open();
Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();

一旦您获得支持的尺寸,您可以使用以下方法定义它们:

setPictureSize(int width, int height);