自定义相机的画质不佳
Posted
技术标签:
【中文标题】自定义相机的画质不佳【英文标题】:Poor picture quality from Custom Camera 【发布时间】:2014-11-12 15:08:13 【问题描述】:目前我正在使用自定义相机应用程序,预览看起来还不错。但是当我拍照并在我的其他活动中显示它时,图片减少了大约 80%。任何人都知道为什么会这样?画廊的质量也很差。 我正在使用来自 android 的 Camera API Demo。
我的参数:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
parameters.setJpegQuality(100);
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
parameters.setRotation(90);
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_0)
mCamera.setDisplayOrientation(90);
else if (display.getRotation() == Surface.ROTATION_270)
mCamera.setDisplayOrientation(180);
mCamera.setParameters(parameters);
mCamera.startPreview();
代码保存图片:
private PictureCallback mPicture = new PictureCallback()
private String TAG = "DocsPro";
@Override
public void onPictureTaken(byte[] data, Camera camera)
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null)
Log.d(TAG, "Error creating media file, check storage permissions : PICTURE FILE IS NULL");
return;
try
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
String filepath = pictureFile.getAbsolutePath();
Intent edit = new Intent(TakePhoto.this, EditPhoto.class);
edit.putExtra("filepath", filepath);
startActivity(edit);
finish();
catch (FileNotFoundException e)
Log.d(TAG, "File not found: " + e.getMessage());
catch (IOException e)
Log.d(TAG, "Error accessing file: " + e.getMessage());
;
private static File getOutputMediaFile(int type)
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "DocsPro");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists())
if (!mediaStorageDir.mkdirs())
Log.d("MyCameraApp", "failed to create directory");
return null;
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE)
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
else
return null;
return mediaFile;
【问题讨论】:
张贴您将图像保存到磁盘的代码 完成,请看原帖 完全未实现的建议-使用异步任务将图像字节写入磁盘reference for writing camera image on to the disk 图片的宽高尺寸是多少? 它已更改为 1944x2592,因为在其中添加了一些新代码。但是它太大了,所以我必须重新调整它才能看到我的新质量。 【参考方案1】:问题已解决: 我只是把它放在我的预览课上改变的表面上。
List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
for(int i=0;i<sizes.size();i++)
if(sizes.get(i).width > size.width)
size = sizes.get(i);
parameters.setPictureSize(size.width, size.height);
然后只调整位图的大小,因为图片太大了:
imageView = (ImageView) findViewById(R.id.Image);
Bitmap bp = BitmapFactory.decodeFile(imagePath);
Bitmap resized = Bitmap.createScaledBitmap(bp,(int)(bp.getWidth()*0.7), (int)(bp.getHeight()*0.7), true);
imageView.setImageBitmap(resized);
【讨论】:
感谢为我工作。我捕获的图像现在非常清晰。还要获得相机的清晰预览 params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);见链接Auto Focus preview以上是关于自定义相机的画质不佳的主要内容,如果未能解决你的问题,请参考以下文章