我制作了自己的自定义相机。这给了我糟糕的像素化图像,我不知道为什么。我使用JPEG压缩100质量。这就是我得到的形象 Camera picture
这是我的代码:
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Context mContext;
private Camera.Parameters parameters;
private byte[] mBuffer;
public CameraSurfaceView(Context context) {
super(context);
mContext = context;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
if (CameraConfigurationUtils.mCameraInstance != null) {
CameraConfigurationUtils.mCameraInstance.setDisplayOrientation(90);
CameraConfigurationUtils.mCameraInstance.setPreviewDisplay(holder);
parameters = CameraConfigurationUtils.mCameraInstance.getParameters();
if (android.os.Build.VERSION.SDK_INT >= 14) {
CameraConfigurationUtils.setFocus(parameters, true, true, false);
} else {
CameraConfigurationUtils.setFocus(parameters, true, true, true);
}
WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point theScreenResolution = new Point();
theScreenResolution.set(display.getHeight(), display.getWidth());
CameraConfigurationUtils.findBestPreviewSizeValue(parameters, theScreenResolution);
CameraConfigurationUtils.mCameraInstance.setParameters(parameters);
updateBufferSize();
CameraConfigurationUtils.mCameraInstance.addCallbackBuffer(mBuffer);
CameraConfigurationUtils.mCameraInstance.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() {
public synchronized void onPreviewFrame(byte[] data, Camera c) {
if (CameraConfigurationUtils.mCameraInstance != null) {
CameraConfigurationUtils.mCameraInstance.addCallbackBuffer(mBuffer);
}
}
});
CameraConfigurationUtils.startPreview(mContext);
}
} catch (IOException e) {
e.printStackTrace();
CameraConfigurationUtils.releaseCamera();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mHolder.removeCallback(this);
CameraConfigurationUtils.stopPreview();
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null) {
return;
}
try {
CameraConfigurationUtils.mCameraInstance.setPreviewDisplay(mHolder);
CameraConfigurationUtils.startPreview(mContext);
} catch (Exception e) {
CameraConfigurationUtils.releaseCamera();
}
}
public byte[] getPic(int x, int y, int width, int height) {
System.gc();
Camera.Size s = parameters.getPreviewSize();
YuvImage yuvimage = new YuvImage(mBuffer, ImageFormat.NV21, s.width, s.height, null);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make JPG
yuvimage = null;
System.gc();
return outStream.toByteArray();
}
private void updateBufferSize() {
mBuffer = null;
System.gc();
// prepare a buffer for copying preview data to
int h = parameters.getPreviewSize().height;
int w = parameters.getPreviewSize().width;
int bitsPerPixel = ImageFormat.getBitsPerPixel(parameters.getPreviewFormat());
mBuffer = new byte[w * h * bitsPerPixel / 8];
//Log.i("surfaceCreated", "buffer length is " + mBuffer.length + " bytes");
}
}
我做错了什么?
答案 0 :(得分:1)
您的图片示例看起来就像来自相机的常规较低分辨率预览图像,但我可以看到在高分辨率屏幕中它会显示像素化。我相信你的问题在于代码的这一部分:
CameraConfigurationUtils.findBestPreviewSizeValue(parameters, theScreenResolution);
CameraConfigurationUtils.mCameraInstance.setParameters(parameters);
看起来您只是在计算最佳预览尺寸,但是您没有将其设置为实际的预览尺寸。试试这样:
Point bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, theScreenResolution);
parameters.setPreviewSize(bestPreviewSize.x, bestPreviewSize.y);
CameraConfigurationUtils.mCameraInstance.setParameters(parameters);
另一件事是,如果您的目的是拍摄高分辨率照片,则不应使用getPic()
方法进行拍摄,因为它只是将预览帧作为jpeg,这是一种较低分辨率的图像。你应该考虑Camera#takePicture method。