gpt4 book ai didi

android - 将图像转换为位图

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:48 25 4
gpt4 key购买 nike

我想创建特定图像的位图图像,但我想抵抗位图图像的宽度和高度。我正在使用以下代码......

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, 0, this.getMeasuredWidth(), this.getMeasuredHeight());

this.buildDrawingCache(true);
Bitmap image = Bitmap.createBitmap(this.getDrawingCache());
this.setDrawingCacheEnabled(false);
//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() );


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

现在它向我显示空指针异常...堆栈跟踪

05-29 15:22:07.205: E/AndroidRuntime(889): FATAL EXCEPTION: main
05-29 15:22:07.205: E/AndroidRuntime(889): java.lang.NullPointerException
05-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 的图像缓存。它将在布局时(缩放、用背景边框等)将整个 View 渲染到新的位图。

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

可能是这样的:

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

More

或将sdcard图像形成位图

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, 0, bMapArray.length

关于android - 将图像转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10796660/

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