gpt4 book ai didi

java - 如何压缩图像大小

转载 作者:行者123 更新时间:2023-12-02 04:14:59 26 4
gpt4 key购买 nike

这是我用来捕获图像的代码,但现在我的问题是如何压缩自定义相机拍摄的图像的大小

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);
}

最佳答案

喜欢

@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);
}

关于java - 如何压缩图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33411552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com