【问题标题】:Set size of the photo taken by camera设置相机拍摄的照片大小
【发布时间】:2011-04-27 07:02:38
【问题描述】:

有什么方法可以不断设置我自己的相机应用程序拍摄的照片的分辨率(如 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();
}

【问题讨论】:

标签: android


【解决方案1】:

有关信息,请参阅Camera Parameters

【讨论】:

  • 我已经尝试过类似 parameters.setPictureSize(256, 256); 的东西。但它强制关闭。
  • 您需要使用getSupportedPictureSizes() 获取支持的尺寸列表,并使用最接近您想要的尺寸。
  • 我已经上传了我的代码,你能告诉我把这些行放在哪里吗?我似乎无法弄清楚。
【解决方案2】:

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

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

一旦你确定了支持的尺寸,你就可以用以下方法定义它们:

setPictureSize(int width, int height);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-30
    • 2017-10-13
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多