android – 将图像转换为位图

android – 将图像转换为位图,第1张

概述我想创建特定图像位图图像,但我想抵制bitmapimage的宽度和高度.我使用以下代码…… image2 = BitmapFactory.decodeResource(getResources(), rid); Log.w("DEBUG","which is null:image i " + i.getWidth() + " OR " +i.getDrawable().getI 我想创建特定图像的位图图像,但我想抵制bitmAPImage的宽度和高度.我使用以下代码……

image2  = BitmapFactory.decodeResource(getResources(),rID);        Log.w("DEBUG","which is null:image i " + i.getWIDth() + " OR " +i.getDrawable().getIntrinsicHeight () );        Log.w("DEBUG","which is null:image h " + h.getWIDth() + " OR " +h.getHeight() );        Log.w("DEBUG","which is null:image2 " + image2.getWIDth() + " OR " +image2.getHeight() );     Bitmap image = Bitmap.createScaledBitmap(image2,i.getWIDth(),i.getHeight(),false);        Log.w("DEBUG","which is null:image " + image.getWIDth() + " OR " +image.getHeight() );        double bmWIDth = image.getWIDth();          double bmHeight = image.getHeight();           if ( x < 0 || y < 0 || x > getWIDth() || y > getHeight()){               return 0; //InvalID,return 0               }else{               //Convert touched x,y on VIEw to on Bitmap               int xBm = (int)(x * (bmWIDth / getWIDth()));               int yBm = (int)(y * (bmHeight / getHeight()));               return image.getPixel(xBm,yBm);

但它并没有给我相同的输出……..它显示了我相同的宽度和高度,但我想要的输出不会来.在我想要的输出高度变得很大……所以如何克服这一点.不能一个人建议我

编辑:

this.setDrawingCacheEnabled(true);        // this is the important code :)          // Without it the vIEw will have a dimension of 0,0 and the bitmap will be null                  this.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UnspecIFIED),MeasureSpec.makeMeasureSpec(0,MeasureSpec.UnspecIFIED));        this.layout(0,this.getMeasureDWIDth(),this.getMeasuredHeight());         this.buildDrawingCache(true);        Bitmap image = Bitmap.createBitmap(this.getDrawingCache());        this.setDrawingCacheEnabled(false);        //image2  = BitmapFactory.decodeResource(getResources(),"which is null:image2 " + image2.getWIDth() + " OR " +image2.getHeight() );        Log.w("DEBUG",yBm);               }}

现在它显示我的空指针异常…
堆栈跟踪

05-29 15:22:07.205: E/AndroIDRuntime(889): FATAL EXCEPTION: main05-29 15:22:07.205: E/AndroIDRuntime(889): java.lang.NullPointerException05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.graphics.Bitmap.createBitmap(Bitmap.java:358)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.example.nam.ImageIn.getmaskPixel(ImageIn.java:60)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.example.nam.AshActivity.getMaskcolor(AshActivity.java:192)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.example.nam.Firstimage.pageinfo(Firstimage.java:102)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.example.nam.Firstimage.ontouch(Firstimage.java:65)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEw.dispatchtouchEvent(VIEw.java:3762)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:863)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:863)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:863)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:863)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:863)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:863)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:863)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.androID.internal.policy.impl.PhoneWindow$DecorVIEw.superdispatchtouchEvent(PhoneWindow.java:1671)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.androID.internal.policy.impl.PhoneWindow.superdispatchtouchEvent(PhoneWindow.java:1107)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.app.Activity.dispatchtouchEvent(Activity.java:2086)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.androID.internal.policy.impl.PhoneWindow$DecorVIEw.dispatchtouchEvent(PhoneWindow.java:1655)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.vIEw.VIEwRoot.handleMessage(VIEwRoot.java:1785)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.os.Handler.dispatchMessage(Handler.java:99)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.os.Looper.loop(Looper.java:123)05-29 15:22:07.205: E/AndroIDRuntime(889):  at androID.app.ActivityThread.main(ActivityThread.java:4627)05-29 15:22:07.205: E/AndroIDRuntime(889):  at java.lang.reflect.Method.invokeNative(Native Method)05-29 15:22:07.205: E/AndroIDRuntime(889):  at java.lang.reflect.Method.invoke(Method.java:521)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)05-29 15:22:07.205: E/AndroIDRuntime(889):  at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:626)05-29 15:22:07.205: E/AndroIDRuntime(889):  at dalvik.system.NativeStart.main(Native Method)
解决方法 你可以使用imageVIEw的图像缓存.它将整个视图渲染(按比例缩放,以背景等为边界)渲染到新的位图.

imageVIEw.buildDrawingCache();Bitmap bmap = imageVIEw.getDrawingCache();

可能是这样的:

BitmapDrawable drawable = (BitmapDrawable) imageVIEw.getDrawable();Bitmap bitmap = drawable.getBitmap();

More

或者将SD卡图像形成为位图

fileinputStream in = new fileinputStream("/sdcard/test2.png");BufferedinputStream buf = new BufferedinputStream(in);byte[] bMapArray= new byte[buf.available()];buf.read(bMapArray);Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray,bMapArray.length
总结

以上是内存溢出为你收集整理的android – 将图像转换为位图全部内容,希望文章能够帮你解决android – 将图像转换为位图所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1124528.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存