我想通过重叠创建具有两个不同图像的组合图像.
为此,我的代码是
ImageView image = (ImageView) findViewById(R.id.imageView1);
Drawable drawableFore = getResources().getDrawable(R.drawable.foreg);
Drawable drawableBack = getResources().getDrawable(R.drawable.backg);
Bitmap bitmapFore = ((BitmapDrawable) drawableFore).getBitmap();
Bitmap bitmapBack = ((BitmapDrawable) drawableBack).getBitmap();
Bitmap scaledBitmapFore = Bitmap.createScaledBitmap(bitmapFore, 35, 35, true);
Bitmap scaledBitmapBack = Bitmap.createScaledBitmap(bitmapBack, 45, 45, true);
Bitmap combineImages = overlay(scaledBitmapBack, scaledBitmapFore);
image.setImageBitmap(combineImages);
Run Code Online (Sandbox Code Playgroud)
overlay()方法是
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
try
{
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
} catch (Exception …Run Code Online (Sandbox Code Playgroud) 我有一张collsion地图,有些地方我想成为光源.光源提供的光实际上是我可以看到地面的形状.它现在看起来像这样:

所以光线穿过墙壁.我想让它看起来像这样:

(我标记了墙壁与深黄色的碰撞)
所以当遇到墙壁时光线会停止.我想获得正确光线的形状,最好的是包含它的位图)
我的第一个想法是从光源投射光线并检查它们何时与墙壁碰撞(我知道如何做到这一点),但是我需要每隔0.001度投射一次光线,所以它太多时间来产生光线.接下来的事情是光的形状并不总是圆形,有时它可以是椭圆形或半椭圆形,甚至是三角形或圆形的一部分.一般来说,我的光点位图不会碰到任何东西,我想稍微减去它以使它看起来像第二张图像.
最后一件事,我使用了allegro 4.2.1,但之前提到的所有位图都是具有0和1的2维度数组.
Thanx任何帮助,抱歉长问题和我的英语不好.
从解锁的Bitmap非托管内存直接写入和读取是否可以?
在我解锁Bitmap后,我可以继续使用BitmapData吗?我做了一个测试应用程序,我可以在鼠标位置读取PictureBox的Bitmap像素,而另一个线程正在将像素写入同一个Bitmap.
编辑1:正如Boing他在回答中指出的那样:"Scan0没有指向Bitmap对象的实际像素数据;而是指向一个临时缓冲区,它代表Bitmap对象中像素数据的一部分." 来自MSDN.
但是一旦我得到了Scan0,我就可以读取/写入Bitmap而无需Lockbits或UnlockBits!我在一个帖子里做了很多次.相对于MSDN,它不应该发生,因为Scan0指向位图数据的COPY!好吧,在C#中,所有测试表明它不是副本.在C++中,我不知道它是否正常工作.
编辑2:使用旋转方法有时会使操作系统释放位图像素数据副本.结论,it is not safe to read/write an unlocked Bitmap Scan0.谢谢Boing的回答和评论!
下面是我如何获取BitmapData并读取和写入像素值.
/// <summary>
/// Locks and unlocks the Bitmap to get the BitmapData.
/// </summary>
/// <param name="bmp">Bitmap</param>
/// <returns>BitmapData</returns>
public static BitmapData GetBitmapData(Bitmap bmp)
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
bmp.UnlockBits(bmpData);
return bmpData;
}
/// <summary>
/// Get pixel directly from unamanged pixel data based on the Scan0 pointer.
/// </summary> …Run Code Online (Sandbox Code Playgroud) 我正在使用这个非常普通的课程来圆角:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Run Code Online (Sandbox Code Playgroud)
我想修改它,以便只有左上角是圆角的.我在代码中找不到这个参数吗?有人可以帮忙吗?
我试图从相机的预览中获取byte [],将其转换为位图并使用imageView.setImageBitmap()在imageview上显示它
我已经设法开始预览并将其显示在surfaceView上,但我不知道如何在RGB位图中转换byte []数据(我认为是Yuv格式)以在imageView上显示它.
我正在尝试的代码如下:
camera = camera.open();
parameters = camera.getParameters();
camera.setParameters(parameters);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
camera.setPreviewDisplay(surfaceHolder);
camera.setPreviewCallback(this);
camera.startPreview();
Run Code Online (Sandbox Code Playgroud)
并且预览回调就是这个
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
ByteArrayOutputStream outstr = new ByteArrayOutputStream();
Rect rect = new Rect(0, 0, width, height);
YuvImage yuvimage=new YuvImage(data,ImageFormat.NV21,width,height,null);
yuvimage.compressToJpeg(rect, 100, outstr);
Bitmap bmp = BitmapFactory.decodeByteArray(outstr.toByteArray(), 0, outstr.size());
imgView1.setImageBitmap(bmp);
}
Run Code Online (Sandbox Code Playgroud)
预览有效但imageView仍为空
任何的想法?
我有一个代码,使用以下代码将具有灰色的位图转换为黑色和白色的位图:
// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = bitmap.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B);
// use 128 as threshold, above -> white, below -> black
if (gray > 128)
gray = 255;
else …Run Code Online (Sandbox Code Playgroud) 我现在正在使用以下代码,它适用于~300个图像,但我必须合并超过一千个.
private static void CombineThumbStripImages(string[] imageFiles)
{
int index = 0;
using (var result = new Bitmap(192 * imageFiles.Length, 112))
{
using (var graphics = Graphics.FromImage(result))
{
graphics.Clear(Color.White);
int leftPosition = 0;
for (index = 0; index < imageFiles.Length; index++)
{
string file = imageFiles[index];
using (var image = new Bitmap(file))
{
var rect = new Rectangle(leftPosition, 0, 192, 112);
graphics.DrawImage(image, rect);
leftPosition += 192;
}
}
}
result.Save("result.jpg", ImageFormat.Jpeg);
}
}
Run Code Online (Sandbox Code Playgroud)
它抛出以下异常:
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred …Run Code Online (Sandbox Code Playgroud) 在设置内容视图时,我遇到了这个异常.我已经尝试通过以下方式处理此异常:
try{
setContentView(R.layout.activity_main);
}catch (OutOfMemoryError e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
在测试时,这个例外在100次约为5次.但我无法解决这个问题.或者我的QA团队将在此杀死我:(
我正在生成视图的位图,以将其显示为地图上的标记.它工作正常,但我面临的问题是视图的高度没有显示.
private Bitmap getMarkerBitmapFromView(View view) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//view.buildDrawingCache();
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
/*canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = view.getBackground();
if (drawable != null)
drawable.draw(canvas);
*/
view.draw(canvas);
return returnedBitmap;
}
Run Code Online (Sandbox Code Playgroud) android bitmap google-maps-markers android-canvas android-elevation
我想截取页面的截图并将结果放入位图,因为页面上有滚动条,所以我必须截取几个截屏,并且希望合并这些位图。
如果已使用此代码制作了屏幕截图并保存,请执行以下操作:拍摄Delphi 7中特定区域的屏幕截图
我使用此代码从此页面合并它们http://www.delphigroups.info/2/8/309463.html
如果我直接复制它,将导致使用第一个图像,第二个图像是白色矩形。所以我尝试对其进行一些更改,现在我将两个图像都保存在一个文件中。
这是我用来连接位图的代码:
function ConcatenateBitmaps(const MainBitmap: TBitmap; const BitmapToAdd:
TBitmap): TBitmap;
begin
Result := MainBitmap;
If BitmapToAdd.Width > MainBitmap.Width then
Result.Width := BitmapToAdd.Width;
Result.Height := MainBitmap.Height + MainBitmap.Height;
Result.Canvas.CopyRect(
Rect(0,MainBitmap.Height,BitmapToAdd.Width,BitmapToAdd.Height),
BitmapToAdd.Canvas,
Rect(0,0,BitmapToAdd.Width,BitmapToAdd.Height)
);
end;
Run Code Online (Sandbox Code Playgroud)
问题是第二个图像被垂直和水平翻转。
我在这里做错了什么?
正如我现在所看到的,我的描述是错误的,它在水平方向上是镜像的,在垂直方向上是翻转的