Android减少相机图片的大小

Android减少相机图片的大小,第1张

概述更新可能更改新活动中的位图大小可能会解决问题 String myRef = this.getIntent().getStringExtra("filepath"); File imgFile = new File(myRef); Log.e("BeatEmUp", myRef); if(imgFile.exists()){ Bitmap myBitma 更新可能更改新活动中的位图大小可能会解决问题
String myRef = this.getIntent().getStringExtra("filepath");    file imgfile = new  file(myRef);    Log.e("BeatEmUp",myRef);    if(imgfile.exists()){        Bitmap myBitmap = BitmapFactory.decodefile(imgfile.getabsolutePath());        ImageVIEw myImage = (ImageVIEw) findVIEwByID(R.ID.imagepunch);        myImage.setimageBitmap(myBitmap);    }

这样的事可能吗?

位图缩放= Bitmap.createScaledBitmap(bit,200,true);

或者这可能是下面最好的方式

到目前为止,我有我的应用程序正在拍照,然后使用Intent将图片带到一个新的活动中.一旦我完成了这个,我有一些代码,用onClick在顶部显示图像.问题是,当拍摄图像时,我认为这是我的应用程序强行关闭.

在我的logcat中我得到java.lang.OutOfMemoryError:位图大小超过VM预算(堆大小= 7431KB,分配= 2956KB,位图大小= 19764KB),我认为这意味着图像太大.

我已经通过在代码中放置一个较小的图像而不是拍摄的相机图像进行测试,这也使我回到问题,即它是相机图片的大小.

所以我试图实现This From CommonsWare但到目前为止没有运气.仔细查看代码,我认为它会搜索最小的resouloution / size,然后使用这些设置拍摄相机我正确地想到了这一点,如果是这样,我怎样才能将其实现到我的代码中?

public class AndroIDCamera extends Activity implements SurfaceHolder.Callback{Camera camera;SurfaceVIEw surfaceVIEw;SurfaceHolder surfaceHolder;boolean prevIEwing = false;LayoutInflater controlinflater = null;final int RESulT_SAVEIMAGE = 0;/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    setRequestedOrIEntation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);    getwindow().setFormat(PixelFormat.UNKNowN);    surfaceVIEw = (SurfaceVIEw)findVIEwByID(R.ID.cameraprevIEw);    surfaceHolder = surfaceVIEw.getHolder();    surfaceHolder.addCallback(this);    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    controlinflater = LayoutInflater.from(getBaseContext());    VIEw vIEwControl = controlinflater.inflate(R.layout.control,null);    LayoutParams layoutParamsControl         = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);    this.addContentVIEw(vIEwControl,layoutParamsControl);    button buttonTakePicture = (button)findVIEwByID(R.ID.takepicture);    buttonTakePicture.setonClickListener(new button.OnClickListener(){        public voID onClick(VIEw arg0) {            // Todo auto-generated method stub            camera.takePicture(myShutterCallback,myPictureCallback_RAW,myPictureCallback_JPG);        }});}ShutterCallback myShutterCallback = new ShutterCallback(){    public voID onShutter() {        // Todo auto-generated method stub    }};PictureCallback myPictureCallback_RAW = new PictureCallback(){    public voID onPictureTaken(byte[] arg0,Camera arg1) {        // Todo auto-generated method stub    }};PictureCallback myPictureCallback_JPG = new PictureCallback(){    public voID onPictureTaken(byte[] arg0,Camera arg1) {        // Todo auto-generated method stub        /*Bitmap bitmapPicture             = BitmapFactory.decodeByteArray(arg0,arg0.length);  */        int imageNum = 0;        Intent imageIntent = new Intent(androID.provIDer.MediaStore.ACTION_IMAGE_CAPTURE);        file imagesFolder = new file(Environment.getExternalStorageDirectory(),"Punch");        imagesFolder.mkdirs(); // <----        String filename = "image_" + String.valueOf(imageNum) + ".jpg";        file output = new file(imagesFolder,filename);        while (output.exists()){            imageNum++;            filename = "image_" + String.valueOf(imageNum) + ".jpg";            output = new file(imagesFolder,filename);        }        Uri uriSavedImage = Uri.fromfile(output);        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriSavedImage);        OutputStream imagefileOS;        try {            imagefileOS = getContentResolver().openOutputStream(uriSavedImage);            imagefileOS.write(arg0);            imagefileOS.flush();            imagefileOS.close();            Toast.makeText(AndroIDCamera.this,"Image saved",Toast.LENGTH_LONG).show();        } catch (fileNotFoundException e) {            // Todo auto-generated catch block            e.printstacktrace();        } catch (IOException e) {            // Todo auto-generated catch block            e.printstacktrace();        }        Intent intent = new Intent(getBaseContext(),Punch.class);        intent.putExtra("filepath",Uri.parse(output.getabsolutePath()).toString());        //just using a request code of zero        int request=0;        startActivityForResult(intent,request);     }};public voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) {    // Todo auto-generated method stub    if(prevIEwing){        camera.stopPrevIEw();        prevIEwing = false;    }    if (camera != null){        try {            camera.setPrevIEwdisplay(surfaceHolder);            camera.startPrevIEw();            prevIEwing = true;        } catch (IOException e) {            // Todo auto-generated catch block            camera.release();            e.printstacktrace();        }    }}public voID surfaceCreated(SurfaceHolder holder) {    // Todo auto-generated method stub    camera = Camera.open();    try {           Camera.Parameters parameters = camera.getParameters();           if (this.getResources().getConfiguration().orIEntation != Configuration.ORIENTATION_LANDSCAPE) {              // This is an undocumented although wIDely kNown feature              parameters.set("orIEntation","portrait");              // For AndroID 2.2 and above              camera.setdisplayOrIEntation(90);              // Uncomment for AndroID 2.0 and above              parameters.setRotation(90);           } else {              // This is an undocumented although wIDely kNown feature              parameters.set("orIEntation","landscape");              // For AndroID 2.2 and above              camera.setdisplayOrIEntation(0);              // Uncomment for AndroID 2.0 and above              parameters.setRotation(0);           }          camera.setParameters(parameters);          camera.setPrevIEwdisplay(holder);      } catch (IOException exception) {         camera.release();       }        camera.startPrevIEw();    }public voID surfaceDestroyed(SurfaceHolder holder) {    // Todo auto-generated method stub    if(prevIEwing && camera != null) {        if(camera!=null) {            camera.stopPrevIEw();            camera.release();              camera = null;        }        prevIEwing = false;    }}}
解决方法

So far I have my app which is taking a picture and then using an Intent to carry the picture over and display in a new Activity.

在大多数情况下,使用Intent附加功能在活动之间传递数据是一个很好的解决方案.但是,大位图会因内存消耗而导致问题.这是我要小心使用静态数据成员的一种情况.

In my logcat I am getting java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7431KB,Allocated=2956KB,Bitmap Size=19764KB) which I think means that the image is too large.

这意味着你没有足够的内存来处理你想做的事情.

Looking through the code I think it searches for the smallest resouloution/size and then takes the camera using those settings am I right in thinking that

是.

if so how can I implement that into my code?

复制和粘贴通常有效. 总结

以上是内存溢出为你收集整理的Android减少相机图片的大小全部内容,希望文章能够帮你解决Android减少相机图片的大小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1133998.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存