当我使用Bitmap :: FromHBITMAP函数创建新的Gdiplus :: Bitmap时,生成的Bitmap是不透明的 - 没有保留原始HBITMAP的部分透明度.
有没有办法从HBITMAP创建一个带来alpha通道数据的Gdiplus :: Bitmap?
我试图使用以下方法在画布上叠加图像:
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
bmOverlay = Bitmap.createBitmap(70, 70, Bitmap.Config.RGB_565);
canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null); //line 179
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
}
Run Code Online (Sandbox Code Playgroud)
但是,我的应用程序不断崩溃,日志显示:
android.graphics.Canvas.throwIfRecycled(Canvas.java:954)的android.graphics.Canvas.drawBitmap(Canvas.java:980)中的java.lang.NullPointerException,位于com.MyApp.overlay(MyApp.java:179)
有人可以帮忙吗?
我正在编写一个应用程序,它以ImageIcons(在JLabel中)读取和显示图像,应用程序需要能够支持jpeg和位图.
对于jpegs,我发现将文件名直接传递给ImageIcon构造函数工作正常(即使显示两个大的jpeg),但是如果我使用ImageIO.read获取图像然后将图像传递给ImageIcon构造函数,我会得到一个OutOfMemoryError( Java Heap Space)读取第二个图像时(使用与之前相同的图像).
对于位图,如果我尝试通过将文件名传递给ImageIcon来读取,则不会显示任何内容,但是通过使用ImageIO.read读取图像然后在ImageIcon构造函数中使用此图像可以正常工作.
我从阅读其他论坛帖子中了解到,两种方法对于不同格式不起作用的原因归结为java与位图的兼容性问题,但是有一种解决我的问题的方法,以便我可以使用相同的方法没有OutOfMemoryError的位图和jpegs?
(如果可能的话,我想避免增加堆大小!)
OutOfMemoryError由此行触发:
img = getFileContentsAsImage(file);
Run Code Online (Sandbox Code Playgroud)
方法定义是:
public static BufferedImage getFileContentsAsImage(File file) throws FileNotFoundException {
BufferedImage img = null;
try {
ImageIO.setUseCache(false);
img = ImageIO.read(file);
img.flush();
} catch (IOException ex) {
//log error
}
return img;
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪是:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:58)
at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:397)
at java.awt.image.Raster.createWritableRaster(Raster.java:938)
at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1056)
at javax.imageio.ImageReader.getDestination(ImageReader.java:2879)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:925)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:897)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1282)
at framework.FileUtils.getFileContentsAsImage(FileUtils.java:33)
Run Code Online (Sandbox Code Playgroud) 我试图序列化一个类,我有一个位图变量.这是有点工作的代码....我需要帮助找出仍然存在的问题.....
private Bitmap myVideoScreenshotBm;
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeInt(myVideoScreenshotBm.getRowBytes());
out.writeInt(myVideoScreenshotBm.getHeight());
out.writeInt(myVideoScreenshotBm.getWidth());
int bmSize = myVideoScreenshotBm.getHeight() * myVideoScreenshotBm.getRowBytes();
ByteBuffer dst= ByteBuffer.allocate(bmSize);
myVideoScreenshotBm.copyPixelsToBuffer(dst);
byte[] bytesar=new byte[bmSize];
dst.position(0);
dst.get(bytesar);
out.write(bytesar);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
int nbRowBytes=in.readInt();
int height=in.readInt();
int width=in.readInt();
//
int bmSize = nbRowBytes * height;
byte[] toread= new byte[bmSize];
in.read(toread, 0, toread.length);
ByteBuffer dst= ByteBuffer.allocate(bmSize);
dst.put(toread);
dst.position(0);
myVideoScreenshotBm=Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
myVideoScreenshotBm.copyPixelsFromBuffer(dst);
}
Run Code Online (Sandbox Code Playgroud)
我没有得到错误,但我得到的位图是错的...另外,我不知道如何知道哪个Bitmap.Config标志是合适的......怎么知道?
任何帮助?
我正在使用安全摄像头DLL从相机中检索图像.DLL调用我的程序的函数传递图像缓冲区作为参数,但图像是yuy2格式.我需要将此缓冲区转换为RGB,但我尝试了在Internet上找到的每个公式都没有成功.我尝试的每个例子(包括http://msdn.microsoft.com/en-us/library/aa904813(VS.80).aspx#yuvformats_2)都给我错误的颜色.
我只能使用像素的Y分量将缓冲区转换为BW图像,但我真的需要彩色图片.我调试(仅汇编)在屏幕上显示图像的DLL,它使用DirectDraw来执行此操作.
我有一些代码,用户在屏幕上绘制内容,我想将其作为PNG存储在byte []中.但是,compress()方法返回false.知道为什么会这样吗?是否有更好的方法来获取byte []?
Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(bm);
c.drawPath(mSignaturePath, mSignaturePaint);
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (bm.compress(Bitmap.CompressFormat.PNG, 100, out)) {
byte[] result = out.toByteArray(); // Never gets called
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我在res/drawable中放了一些位图.
在我通过BitmapFactory.decodeResource()加载位图后,我发现根据密度自动调整它们的大小.
这不是我想要的.我希望他们保持原始大小(像素大小).
我该怎么办?
我正在尝试子类化ImageView并绘制一些东西Bitmap.不过,我不能找到一个办法让Rect这Bitmap绘制.我只能得到Rect这ImageView是绘制getDrawingRect(Rect)的方法ImageView.下面是我想要的例子:

我想要的Rect是蓝色的.提前致谢.
当我试图将位图保存到外部图片目录时,我有一个问题.当我使用Bitmap.compress函数保存它时,位图失去透明度并使背景变黑.但是当我将位图传递给imageview并在活动中显示它时它看起来很好并且具有透明度.只有当我尝试保存它时,透明度才会变黑.
我不得不说,我使用两个位图和porterduff模式在位图上绘制路径并仅显示绘制路径中的图片,并且所有其他像素应该被剪切或透明.
所以这是创建路径位图的功能:
private void createPathBitmap(RectF rect, Bitmap bitmap, Path path) {
RectF tmpRect = new RectF(rect);
Bitmap src = Bitmap.createBitmap(bitmap, (int) tmpRect.left, (int) tmpRect.top, (int) tmpRect.width(), (int) tmpRect.height());
Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dst);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(mDisplayDensity * SnippetLayer.PATH_DIAMETER);
path.offset(-rect.left, -rect.top);
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
RectF srcRect = new RectF(0, 0, rect.width(), rect.height());
canvas.drawBitmap(src, null, srcRect, paint);
BitmapManager.sBitmapSnippet = dst;
}
Run Code Online (Sandbox Code Playgroud)
这是将位图保存到外部存储器的方法:
SimpleDateFormat dateFormat = new SimpleDateFormat("HH_mm_ss_dd_MM_yyyy"); …Run Code Online (Sandbox Code Playgroud) 我有一个基于opencv用C++编写的图像处理功能.在我的wpf应用程序中,我使用AForge库来访问网络摄像头并在UI上更新它.这是处理新帧的功能.
void UI_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
System.Drawing.Bitmap bitmapFrame = (Bitmap)eventArgs.Frame.Clone();
MemoryStream ms = new MemoryStream();
bitmapFrame.Save(ms, ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
BitmapImage bitmapImageFrame = new BitmapImage();
bitmapImageFrame.BeginInit();
bitmapImageFrame.StreamSource = ms;
bitmapImageFrame.EndInit();
bitmapImageFrame.Freeze();
CurrentFrame = bitmapImageFrame;
}
catch (Exception ex)
{
Debug.WriteLine("fatal::" + ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的c ++代码:
void FaceTracker(unsigned char* imageBuffer, int width, int height){
Mat frame(Size(width, height), CV_8UC4, imageBuffer, Mat::AUTO_STEP);
Mat gray;
cvtColor(frame, gray, COLOR_BGR2GRAY);
//do more operations
}
Run Code Online (Sandbox Code Playgroud)
我有两个选择.我已经找到了一个用于将数据复制BitmapImage到IntPtr缓冲区的代码CopyPixels.我测试了它,它工作正常.但我也读过我可以简单地使用句柄传递给Bitmap GetHBitmap.但这不适合我.我读了这个 …