捕获的图像分辨率过大 [英] Captured Image Resolution is too big
问题描述
我在做什么?
我允许用户捕获图像,存储成SD卡,并上传到服务器。
不过,拍摄的图像的宽度越来越分辨率:4608像素,高度:2592像素
现在我要怎么办?
我如何得到小分辨率图像,而不带任何质量妥协,如最小分辨率我可以获取或设置原始图像分辨率拍摄的图像分辨率的25%。(设备特定)
CameraCaptureActivity.java:
公共类CameraCaptureActivity扩展活动实现OnClickListener,PictureCallback {
CameraSurfaceView cameraSurfaceView;
按钮shutterButton;
的SimpleDateFormat SimpleDateFormat的;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_camera_capture);
的FrameLayout preVIEW =(的FrameLayout)findViewById(R.id.camera_ preVIEW);
cameraSurfaceView =新CameraSurfaceView(本);
preview.addView(cameraSurfaceView);
shutterButton =(按钮)findViewById(R.id.shutter_button);
shutterButton.setOnClickListener(本);
}
@覆盖
公共无效的onClick(视图v){
拍照片();
}
私人无效takePicture(){
shutterButton.setEnabled(假);
cameraSurfaceView.takePicture(本);
}
@覆盖
公共无效onPictureTaken(byte []的数据,摄像头摄像头){
// TODO一些与图像数据
文件pictureFileDir = GETDIR();
如果(pictureFileDir.exists()及!&安培;!pictureFileDir.mkdirs()){
返回;
}
SimpleDateFormat的=新的SimpleDateFormat(YYYYMMDDHHMMSS);
strDateFormat = simpleDateFormat.format(新日期());
字符串photoFile =Latest.jpg;
字符串文件名= pictureFileDir.getPath()+文件分割符+ photoFile;
文件PictureFile的=新的文件(文件名);
尝试 {
FileOutputStream中FOS =新的FileOutputStream(PictureFile的);
fos.write(数据);
fos.close();
Toast.makeText(CameraCaptureActivity.this,图像保存:+ photoFile,
Toast.LENGTH_LONG).show();
//重新启动preVIEW并重新启用快门按钮,这样我们就可以拍摄另一张照片
camera.stop preVIEW();
shutterButton.setEnabled(假);
}赶上(异常错误){
Toast.makeText(CameraCaptureActivity.this,图像无法被保存。
Toast.LENGTH_LONG).show();
}
}
私人文件GETDIR(){
文件sdDir =环境
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
返回新的文件(sdDir,最新型号);
}
公共无效onResume()
{
super.onResume();
shutterButton.setEnabled(真正的);
}
}
CameraSurfaceView.java: -
类CameraSurfaceView扩展了SurfaceView实现SurfaceHolder.Callback {
摄像头摄像头;
CameraSurfaceView(上下文的背景下){
超(上下文);
SurfaceHolder支架= this.getHolder();
holder.addCallback(本);
}
@覆盖
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,
INT高度){
camera.setDisplayOrientation(90);
camera.start preVIEW();
}
@覆盖
公共无效surfaceCreated(SurfaceHolder持有者){
尝试 {
//在preVIEW模式打开相机
this.camera = Camera.open();
this.camera.set previewDisplay(保持器);
}赶上(IOException异常IOE){
ioe.printStackTrace(的System.out);
}
}
@覆盖
公共无效surfaceDestroyed(SurfaceHolder持有者){
camera.stop preVIEW();
camera.release();
摄像头= NULL;
}
@覆盖
保护无效onLayout(布尔改变,诠释L,INT T,INT R,int b)在{
// TODO自动生成方法存根
}
公共无效takePicture(PictureCallback imageCallback){
camera.takePicture(NULL,NULL,imageCallback);
}
}
写入数据后,使用FileOutputStream.flush()...
文件文件=新的文件(fileName.toString());
尝试 {
的FileOutputStream流=新的FileOutputStream(文件);
bitmap.com preSS(比较pressFormat.PNG,100,流);
stream.flush();
stream.close();
}赶上(例外五){
// TODO:处理异常
}
您可以保存位图图像下面code
位图照片=(位图)位图图像;
照片= Bitmap.createScaledBitmap(照片,100,100,FALSE);
ByteArrayOutputStream字节=新ByteArrayOutputStream();
photo.com preSS(Bitmap.Com pressFormat.JPEG,40个字节);
文件F =新的文件(Environment.getExternalStorageDirectory()
文件分割符+ +Imagename.jpg);
f.createNewFile();
FileOutputStream中FO =新的FileOutputStream(F);
fo.write(bytes.toByteArray());
fo.close();
What I am Doing ?
I am allowing user to capture image, storing it into SD Card and uploading to server.
But getting resolution of Captured image as Width: 4608 pixels and Height: 2592 pixels
Now What I Want ?
How do I get small resolution images without any compromising with quality, like minimum resolution i can get or set captured image resolution 25% of original image resolution.. (Device Specific)
CameraCaptureActivity.java:
public class CameraCaptureActivity extends Activity implements OnClickListener, PictureCallback {
CameraSurfaceView cameraSurfaceView;
Button shutterButton;
SimpleDateFormat simpleDateFormat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_capture);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
cameraSurfaceView = new CameraSurfaceView(this);
preview.addView(cameraSurfaceView);
shutterButton = (Button) findViewById(R.id.shutter_button);
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 = "Latest.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, "Latest Models");
}
public void onResume()
{
super.onResume();
shutterButton.setEnabled(true);
}
}
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.takePicture(null, null, imageCallback);
}
}
use FileOutputStream.flush() after writing data...
File file = new File(fileName.toString());
try {
FileOutputStream stream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, stream);
stream.flush();
stream.close();
} catch (Exception e) {
// TODO: handle exception
}
You can save Bitmap image following code
Bitmap photo = (Bitmap) "your Bitmap image";
photo = Bitmap.createScaledBitmap(photo, 100, 100, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Imagename.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
这篇关于捕获的图像分辨率过大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!