将图像转换为位图

7

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

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)

请检查这是否是您的问题 - https://dev59.com/w2sy5IYBdhLWcg3w9ixu - Shaiful
你是在onCreate()方法中直接执行这个操作还是在任何点击事件中执行的? - Andro Selva
3个回答

4
你可以使用imageView的图像缓存来实现。它会将整个视图呈现为新的位图(经过缩放、边框处理和背景等布局) 。
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

也许还包括以下内容:
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

更多

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

你能给我解释一下关于sdcardimage转换为位图的内容吗?这会更有助于我的理解,因为我是初学者。 - Ashishsingh

0

如果我理解正确,您想从另一个位图创建一个缩放的位图。以下是一段代码片段,可以让您调整位图大小。

只需输入原始位图对象和所需的位图尺寸,此方法将返回新调整大小的位图!

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();

int height = bm.getHeight();

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

}

获取触摸点的像素值

在位图对象上使用Bitmap.getPixel(int x, int y)。它将返回一个整数值,您需要通过向右移位来分离a、r、g、b值:

int pixelCol = bmp.getPixel(touchX,touchY);

          int a = (pixelCol >>> 24) & 0xff;
          int r = (pixelCol >>> 16) & 0xff;
          int g = (pixelCol >>> 8) & 0xff;
          int b = pixelCol & 0xff;

你想调整位图的大小吗?这是你想要的吗? - K_Anas
基本上在这个函数中,我希望当用户触摸该像素时,它应返回像素的颜色...但是当我转换为位图时,它的高度变大了,但宽度仍然正常。 - Ashishsingh
我知道,但位图图像转换后并没有给我正确的值,而且位图图像已经被正确转换了…… - Ashishsingh

0

你试过禁用自动缩放吗?

Matrix matrix = new Matrix();               
BitmapFactory.Options bfoOptions = new BitmapFactory.Options(); 
//Set to UnAutoScale
bfoOptions.inScaled = false;
//Fit to your size
matrix.setScale(widthRatio, heightRatio); 
Bitmap tempBitmap = BitmapFactory.decodeResource(res, resID, bfoOptions);
Bitmap outputbitmap = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight(), matrix, true);

它显示以下错误java.lang.IllegalArgumentException: bitmap size exceeds 32bits - Ashishsingh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接