如何压缩图像大小
Posted
技术标签:
【中文标题】如何压缩图像大小【英文标题】:How Do I compress Size of Image 【发布时间】:2016-01-29 10:28:43 【问题描述】:这是我用来捕捉图像的代码,但现在我的问题是如何压缩自定义相机拍摄的图像大小
CameraSurfaceView.java:
class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
Camera camera;
CameraSurfaceView(Context context)
super(context);
SurfaceHolder holder = this.getHolder();
holder.addCallback(this);
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
camera.setDisplayOrientation(90);
camera.startPreview();
@Override
public void surfaceCreated(SurfaceHolder holder)
try
// Open the Camera in preview mode
this.camera = Camera.open();
this.camera.setPreviewDisplay(holder);
catch (IOException ioe)
ioe.printStackTrace(System.out);
@Override
public void surfaceDestroyed(SurfaceHolder holder)
camera.stopPreview();
camera.release();
camera = null;
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
// TODO Auto-generated method stub
public void takePicture(PictureCallback imageCallback)
Camera.Parameters params = camera.getParameters();
params.setRotation(90);
camera.setParameters(params);
camera.takePicture(null, null, imageCallback);
这是我用来捕获图像的类
CameraCaptureActivity.java:
CameraSurfaceView cameraSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_capture);
// set up our preview surface
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
cameraSurfaceView = new CameraSurfaceView(this);
preview.addView(cameraSurfaceView);
// grab out shutter button so we can reference it later
shutterButton = (Button) findViewById(R.id.btnCapture);
shutterButton.setOnClickListener(this);
@Override
public void onClick(View v)
takePicture();
private void takePicture()
shutterButton.setEnabled(false);
cameraSurfaceView.takePicture(this);
@Override
public void onPictureTaken(byte[] data, Camera camera)
// TODO something with the image data
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs())
return;
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
strDateFormat = simpleDateFormat.format(new Date());
String photoFile = strRecordName+strDateFormat+".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File pictureFile = new File(filename);
try
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(CameraCaptureActivity.this, "Image saved:" + photoFile,
Toast.LENGTH_LONG).show();
// Restart the preview and re-enable the shutter button so that we can take another picture
camera.stopPreview();
shutterButton.setEnabled(false);
catch (Exception error)
Toast.makeText(CameraCaptureActivity.this, "Image could not be saved.",
Toast.LENGTH_LONG).show();
private File getDir()
File sdDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(sdDir, "CustomCamera");
public void onResume()
super.onResume();
shutterButton.setEnabled(true);
【问题讨论】:
在哪里使用这个? @MD 【参考方案1】:喜欢
@Override
public void onPictureTaken(byte[] data, Camera camera)
Bitmap pic = BitmapFactory.decodeByteArray(data, 0, data.length);
//Rotate the image by 90 degrees before we save it
Matrix matrix = new Matrix();
matrix.postRotate(90);
pic = Bitmap.createBitmap(pic , 0, 0, pic.getWidth(), pic.getHeight(), matrix, true);
........
FileOutputStream fos = new FileOutputStream(pictureFile);
pic.compress(Bitmap.CompressFormat.PNG, 90, fOut);
【讨论】:
@Sophie Right 试试看,我认为您需要在加载图像时进行压缩 @Sophie 只是缩小了您的位图,然后加载到您的图像视图。img.setImageBitmap(Bitmap.createScaledBitmap(pic,210, 210, true));
以上是关于如何压缩图像大小的主要内容,如果未能解决你的问题,请参考以下文章