我正在开发一个应用程序,它在一堆图像上绘制线条.为了选择这些图像,我有一个广播组,每当用户点击一个单选按钮时,图像就会加载所有图纸.
在我的收音机听众中,我有以下代码:
bitmap = BitmapUtils.decodeSampledBitmapFromResource(root + DefinesAndroid.CAMINHO_SHOPPINGS_SDCARD + nomeImagemAtual, size.x, size.y);
mImage.setImageBitmap(bitmap);
mImage.setDrawLines(true);
mImage.setImageBitmap(loadBitmapFromView(mImage));
Run Code Online (Sandbox Code Playgroud)
decodeSampledBitmapFromResource我从Android开发人员的这个链接获得的方法(它更有效地加载位图)http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
这是我调用以获取视图位图的方法
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Run Code Online (Sandbox Code Playgroud)
我正在设置图像Bitmap,mImage因为我正在使用ImageViewTouch库(它可以在ImageView上进行缩放缩放),如果我不这样做,所有画布绘图都会被删除,图像上的任何交互都会被删除(比如放大/出).
错误日志如下
07-11 21:13:41.567: E/AndroidRuntime(20056): java.lang.IllegalArgumentException: width and height must be > 0
07-11 21:13:41.567: E/AndroidRuntime(20056): at android.graphics.Bitmap.createBitmap(Bitmap.java:638)
07-11 21:13:41.567: E/AndroidRuntime(20056): at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
Run Code Online (Sandbox Code Playgroud)
我几乎可以肯定这个错误正在发生,因为当我调用getBitmapFromView方法时图像位图没有完全加载.
如何知道视图何时完全加载?
I am confused what's the different between System.Drawing.Image and System.Drawing.Bitmap
Can someone explain the major difference between those two types ?
为什么要使用System.Drawing.Bitmap而不是System.Drawing.Image?
我正在尝试编写一个轻量级图像查看应用程序.但是,.NET存在系统内存限制.
当尝试加载大位图(9000 x 9000 px或更大,24位)时,我得到一个System.OutOfMemoryException.这是在具有2GB RAM(其中1.3GB用完)的Windows 2000 PC上.尝试加载文件也需要很长时间.
以下代码生成此错误:
Image image = new Bitmap(filename);
using (Graphics gfx = this.CreateGraphics())
{
gfx.DrawImage(image, new Point(0, 0));
}
Run Code Online (Sandbox Code Playgroud)
这段代码也是如此:
Stream stream = (Stream)File.OpenRead(filename);
Image image = Image.FromStream(stream, false, false);
using (Graphics gfx = this.CreateGraphics())
{
gfx.DrawImage(image, new Rectangle(0, 0, 100, 100), 4000, 4000, 100, 100, GraphicsUnit.Pixel);
}
Run Code Online (Sandbox Code Playgroud)
此外,它就足以做到这一点:
Bitmap bitmap = new Bitmap(filename);
IntPtr handle = bitmap.GetHbitmap();
Run Code Online (Sandbox Code Playgroud)
后一个代码旨在与GDI一起使用.在研究这个问题时,我发现这实际上是一个内存问题,.NET试图在单个内存块中分配两倍的内存.
http://bytes.com/groups/net-c/279493-drawing-large-bitmaps
我从其他应用程序(Internet Explorer,MS Paint等)了解到,可以打开大图像,而且很快.我的问题是,如何在.NET中使用大型位图?
无论如何流式传输它们,还是非内存加载它们?
我正在试图弄清楚如何在android中绘制位图,并为撤消函数保留这些已更改位图的副本.
Bitmap b = ...
Paint p = new Paint();
canvas.drawBitmap(b, new Matrix(), null);
canvas.drawCircle(0,0,20,20);
//does Bitmap b have the circle drawn on it next time?
Run Code Online (Sandbox Code Playgroud)
或者如何在使用画布绘制位图之后获取位图(我想保留一堆位图以及画布绘制所应用的更改)?也许我说这完全错了.
我使用以下内容将jpgImage写入PictureBox.Image.
var jpgImage = new Byte[jpgImageSize];
...
pictureBox.Image = new Bitmap(new MemoryStream(jpgImage));
Run Code Online (Sandbox Code Playgroud)
我可以使用以下内容将字节数组写入文件
using (var bw =
new BinaryWriter(File.Open(filename, FileMode.Create,
FileAccess.Write, FileShare.None)))
{
bw.Write(jpgImage);
}
Run Code Online (Sandbox Code Playgroud)
但是如何从PictureBox.Image中获取jpgImage字节数组,以便将其写入文件?IOW:如何反转以下内容以从PictureBox.Image获取字节数组?
pictureBox.Image = new Bitmap(new MemoryStream(jpgImage));
Run Code Online (Sandbox Code Playgroud) 我似乎无法弄清楚这一点.我有2个具有不同特征的java类,每个类调用BitmapFactory.decodeResource来获取相同的图像资源,一个返回位图而另一个返回null.两个类都在同一个包中.
这是有效的类,它调用BitmapFactory.decodeResource返回位图.我只包含相关代码.
package advoworks.test;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainScreen extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = MainScreen.class.getSimpleName();
public MainScreen(Context context) {
super(context);
Bitmap bitmap;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
//adding the callback (this) to the surface holder to intercept events;
getHolder().addCallback(this);
// make the GamePanel focusable so it can handle events
setFocusable(true);
}
}
Run Code Online (Sandbox Code Playgroud)
这是不起作用的类.BitmapFactory.decodeResource在调试中返回NULL.我只包括我觉得相关的代码.
package advoworks.test;
import android.content.res.Resources;
import android.graphics.Bitmap; …Run Code Online (Sandbox Code Playgroud) 我有Image Gridview,我想逐个添加图像到gridview选择从相机中捕获的图库或图像.
如何使用Glide库将Bitmap加载到我的ImageView中?我想用文本创建自定义图像,并使用Glide将其加载到imageview中.
这是我用文本创建自定义位图的方法
public Bitmap imageWithText(String text) {
TextView tv = new TextView(context);
tv.setText(text);
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.BLACK);
tv.setTypeface(null, Typeface.BOLD);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);
tv.setPadding(0, 25, 0, 0);
Bitmap testB = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.layout(0, 0, 100, 100);
tv.draw(c);
return testB;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用滑动加载此位图时,我收到错误
Glide.with(getContext()).load(imageWithText("Random text")).into(holder.imgPhoto);
Run Code Online (Sandbox Code Playgroud) 我正在进行一个屏幕共享项目,我不断收到一小块图像Socket,需要在我拥有的某个初始dekstop位图上更新它们.
基本上我不断地从插座(其被存储为JPEG图像数据)读出的数据,使用Image.FromStream()以检索图像和复制收到块象素到全初级位图(在特定的位置X和Y其我也从插座获得) -这就是如何初始图像得到更新.但接下来是我需要在Picturebox
我处理Paint事件并再次重新绘制它的部分 - 整个初始图像,这是非常大的(在我的情况下是1920X1080).
这是我的代码:
private void MainScreenThread()
{
ReadData();//reading data from socket.
initial = bufferToJpeg();//first intial full screen image.
pictureBox1.Paint += pictureBox1_Paint;//activating the paint event.
while (true)
{
int pos = ReadData();
x = BlockX();//where to draw :X
y = BlockY();//where to draw :Y
Bitmap block = bufferToJpeg();//constantly reciving blocks.
Draw(block, new Point(x, y));//applying the changes-drawing the block on the big initial image.using native memcpy.
this.Invoke(new …Run Code Online (Sandbox Code Playgroud) 我试图拍照然后使用照片.这就是我做的.
我的设备是Nexus 6P(Android 7.1.1).
首先,我创建了一个Uri:
Uri mPicPath = UriUtil.fromFile(this, UriUtil.createTmpFileForPic());
//Uri mPicPath = UriUtil.fromFile(this, UriUtil.createFileForPic());
Run Code Online (Sandbox Code Playgroud)
然后,我开始Intent:
Intent intent = ActivityUtils.getTakePicIntent(mPicPath);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, RequestCode.TAKE_PIC);
}
Run Code Online (Sandbox Code Playgroud)
最后,我处理了这个Uri问题onActivityResult:
if (requestCode == RequestCode.TAKE_PIC) {
if (resultCode == RESULT_OK && mPicPath != null) {
Bitmap requireBitmap = BitmapFactory.decodeFile(mPicPath.getPath());
//path is like this: /Download/Android/data/{@applicationId}/files/Pictures/JPEG_20170216_173121268719051242.jpg
requireBitmap.recycle();//Here NPE was thrown.
}
}
Run Code Online (Sandbox Code Playgroud)
在此期间,以下是UriUtil:
public class UriUtil {
public static File createFileForPic() throws …Run Code Online (Sandbox Code Playgroud) android bitmap android-camera-intent android-fileprovider android-7.0-nougat