Android 将图像转换为位图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10796660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:00:20  来源:igfitidea点击:

Convert image to bitmap

androidimage-processingbitmap

提问by Ashishsingh

I want to create a bitmap image of particular image but i want to resist the width and height of the bitmapimage. i am using the following code ......

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

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

but it is not returning me the same output ........it is showing me the same width and height but my desired output is not coming. in my desired output height is get large ......so how to overcome that. can nay one suggest me

但它没有返回相同的输出......它向我显示相同的宽度和高度,但我想要的输出没有出现。在我想要的输出高度变大......那么如何克服这一点。谁能建议我

edited :

编辑:

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

now it is showing me null pointer exception... stacktrace

现在它向我展示了空指针异常... stacktrace

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)

回答by Ashishsingh

You could just use the imageView's image cache. It will render the entire view as it is layed out (scaled,bordered with a background etc) to a new bitmap.

您可以只使用 imageView 的图像缓存。它会将整个视图在布局(缩放、背景边界等)时呈现为新的位图。

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

and may be this:

可能是这样的:

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

More

更多的

or form sdcard image to bitmap

或将 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

回答by K_Anas

If i have understand you you want to create a scaled bitmap from another.Below is a snippet of code that will allow you to resize a Bitmap.

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

Simply input the original Bitmap object and the desired dimensions of the Bitmap, and this method will return to you the newly resized Bitmap!

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

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;

}

To get the pixel values of touched point

获取触摸点的像素值

Use Bitmap.getPixel(int x, int y) on your bitmap object. It will return you the int value from where you need to take out separate a,r,g,b values by shifting bits to the right:

在位图对象上使用 Bitmap.getPixel(int x, int y)。它将返回您需要通过向右移动位来取出单独的 a,r,g,b 值的 int 值:

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;

回答by John

Do you try to disable the auto-scale?

您是否尝试禁用自动缩放?

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