21

我正在实现一个应用内摄像头,每次拍照时,我的图像都是 320x240,尽管我手机的摄像头能够提供更高的分辨率(Galaxy Nexus)。我找不到用于设置分辨率的参数,那么如何提高我正在拍摄的图像的分辨率?以下是相关代码:

@Override
public void surfaceCreated( SurfaceHolder holder ) {

    setSurfaceHolder( holder );
}

@Override
public void surfaceDestroyed( SurfaceHolder holder ) {

    setSurfaceHolder( null );
}

private void attachCameraToSurface( Camera camera, SurfaceHolder holder ) {

    try {
        camera.setPreviewDisplay( holder );
        setInPreview( true );
    }
    catch (IOException e) {
        Log.e( "CameraFragment", "Exception in attachCameraToSurface()", e );
    }
}

@Override
public void surfaceChanged( SurfaceHolder holder, int format, int width, int height ) {

    Camera camera = getCamera();
    if ( camera == null ) {
        return;
    }

    Camera.Size size = getCameraPreviewSize( camera );
    Camera.Parameters params = camera.getParameters();
    params.setPreviewSize( size.width, size.height );
    camera.setParameters( params );

    attachCameraToSurface( camera, holder );

    startPreview();
}

private Camera.Size getCameraPreviewSize( Camera camera ) {

    Camera.Parameters params = camera.getParameters();
    List<Camera.Size> supportedSizes = params.getSupportedPreviewSizes();

    Rect frame = getSurfaceHolder().getSurfaceFrame();
    int width = frame.width();
    int height = frame.height();

    for (Camera.Size size : supportedSizes) {
        if ( size.width >= width || size.height >= height ) {
            return size;
        }
    }

    return supportedSizes.get( 0 );
}

@Override
public void onClick( View v ) {

    switch (v.getId()) {
        case R.id.camera_action_ImageView:
            getCamera().takePicture( getShutterCallback(), null, new JpegPictureCallback() );
        break;
    }
}

private class JpegPictureCallback implements PictureCallback {

    @Override
    public void onPictureTaken( byte[] data, Camera camera ) {

        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeByteArray( data, 0, data.length );
        }
        catch (OutOfMemoryError e) {
            Log.e( "CameraFragment", "Out of memory decoding image from camera.", e );
            return;
        }
        data = null;

        if ( bitmap != null ) {
            new SavePictureWorker( getActivity() ).execute( bitmap );
        }

        startPreview();
    }
}
4

5 回答 5

38

使用params.setPictureSize(). 有关详细信息,请参阅Camera.Parameters

确保您params.getSupportedPictureSizes()首先调用以获取支持的图片大小数组。然后从列表中选择一个尺寸并使用该setPictureSize()方法设置所需的尺寸。

Camera.Parameters params = camera.getParameters();
List<Camera.Size> supportedSizes = params.getSupportedPictureSizes();
.
.
Camera.Size sizePicture = (one of the sizes from supportedSizes list);
.
params.setPictureSize(sizePicture.width, sizePicture.height);
于 2012-05-15T17:23:34.207 回答
10
Camera.Parameters param;
param = camera.getParameters();

Camera.Size bestSize = null;
List<Camera.Size> sizeList = camera.getParameters().getSupportedPreviewSizes();
bestSize = sizeList.get(0);
for(int i = 1; i < sizeList.size(); i++){
    if((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)){
        bestSize = sizeList.get(i);
    }
}

List<Integer> supportedPreviewFormats = param.getSupportedPreviewFormats();
Iterator<Integer> supportedPreviewFormatsIterator = supportedPreviewFormats.iterator();
while(supportedPreviewFormatsIterator.hasNext()){
    Integer previewFormat =supportedPreviewFormatsIterator.next();
    if (previewFormat == ImageFormat.YV12) {
        param.setPreviewFormat(previewFormat);
    }
}

param.setPreviewSize(bestSize.width, bestSize.height);

param.setPictureSize(bestSize.width, bestSize.height);

camera.setParameters(param);
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
于 2014-11-01T05:06:26.250 回答
3

确保首先调用 camera.getSupportedPictureSizes() 以获取支持的图片尺寸数组。

其实是params.getSupportedPictureSizes();

于 2013-08-22T08:11:25.790 回答
2

我认为,您可以使用setPictureSize (int width, int height). 它用于以像素为单位设置图像尺寸。

于 2012-05-15T17:27:02.080 回答
1
Camera.Parameters parameters = camera.getParameters();
            List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
            int w = 0, h = 0;
            for (Camera.Size size : sizes) {
                if (size.width > w || size.height > h) {
                    w = size.width;
                    h = size.height;
                }

            }
            parameters.setPictureSize(w, h);
于 2017-05-03T10:01:19.280 回答