我想知道是否可以在将位图加载到内存中之后仅绘制一部分位图而不创建新的位图.我看到Drawable有一个setBounds方法,但我不确定它是否只绘制区域集或只调整整个图像的大小.谢谢.
我正在以下列方式压缩位图
Bitmap bmpSig = getMyBitMap();
int size = bmpSig.getWidth() * bmpSig.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bmpSig.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] bytSig = out.toByteArray();
Run Code Online (Sandbox Code Playgroud)
然后我尝试从字节数组中在Android ImageView中显示图像.当我这样做时,我得到的图像是完全黑色的图像.
ImageView myImg = (ImageView) findViewById(R.id.img_view);
myImg.setImageBitmap(BitmapFactory.decodeByteArray(bytSig, 0, bytSig.length));
Run Code Online (Sandbox Code Playgroud)
我假设是因为我在BitmapFactory.decodeByteArray()之前错过了一个步骤来反转jpeg压缩.或者我误解了压缩是如何工作的?
我想从手机的SD卡中选择一张图片.我使用下面的代码来选择并显示在我的活动中
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Uri uri = Uri.parse(selectedImagePath);
uploadimage.setImageURI(uri);
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我想将此图像转换为Bitmap,我有图像路径和URI.
在这种情况下如何将图像转换为位图?请帮助我,提前谢谢.
我开始使用Osmdroid,我想使用这项技术来显示关于F1赛道的地图.我有一张大图,我可以把它切成小块.
我可以修改osmdroid库来上传这些图片吗?
我想在我的资源文件夹中保存这些位图(tile).
我很失落如何做到这一点.
我正在使用这种方式,因为当我尝试显示这个完整的全尺寸图片时,android通常会出现内存错误.如果我只显示图像的一些图块(取决于缩放),也许我的应用程序可以更好地工作?
提前致谢!
问题描述:
我已经测试了以下两种方法,以便从View创建一个位图snaphot(例如在这种情况下为RelativeLayout).这两种方法都适用于视图,其尺寸(例如宽度和高度)小于设备屏幕的尺寸(例如480x900).捕获视图的可见部分并将其写入位图.不幸的是,在View的不可见部分中,位图是黑色的.
问题:
我如何捕捉视图中不可见的部分?
码:
public class NewRelativeLayout extends RelativeLayout{
private Bitmap bmp;
public NewRelativeLayout(Context context){
super(context);
this.setLayoutParams(new RelativeLayout.LayoutParams(2000,2000));
// add here methods to configure the RelativeLayout or to add children
}
//This is the first method
public void makeSnapshot_Method1(){
this.setDrawingCacheEnabled(true);
bmp = Bitmap.createBitmap(this.getDrawingCache());
this.setDrawingCacheEnabled(false);
}
//This is the second method
public void makeSnapshot_Method2(){
bmp = Bitmap.createBitmap(2000, 2000, Bitmap.Config.ARGB_8888);
Canvas helpCanvas = new Canvas(bmp);
this.draw(helpCanvas);
}
Run Code Online (Sandbox Code Playgroud)
}
我想创建一个GraphicsPath和一个Points列表,以形成位图的非透明区域的轮廓.如果需要,我可以保证每个图像只有一个非透明像素的固体集合.因此,例如,我应该能够沿着像素的边缘顺时针或逆时针记录点并执行完全闭环.
该算法的速度并不重要.但是,如果我可以在较小且较不复杂的GraphicsPath中跳过某些点来减少,那么结果点的效率是非常重要的.
我将列出下面的当前代码,它与大多数图像完美配合.但是,一些更复杂的图像最终会出现以错误顺序连接的路径.我想我知道为什么会这样,但我无法想出解决方案.
public static Point[] GetOutlinePoints(Bitmap image)
{
List<Point> outlinePoints = new List<Point>();
BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte[] originalBytes = new byte[image.Width * image.Height * 4];
Marshal.Copy(bitmapData.Scan0, originalBytes, 0, originalBytes.Length);
for (int x = 0; x < bitmapData.Width; x++)
{
for (int y = 0; y < bitmapData.Height; y++)
{
byte alpha = originalBytes[y * bitmapData.Stride + 4 * x + 3];
if (alpha != 0)
{
Point p = new Point(x, y); …Run Code Online (Sandbox Code Playgroud) 当我使用copyPixelsFromBuffer和copyPixelsToBuffer时,位图没有显示为相同的位图,我试过下面的代码:
Bitmap bm = BitmapFactory.decodeByteArray(a, 0, a.length);
int[] pixels = new int[bm.getWidth() * bm.getHeight()];
bm.getPixels(pixels, 0, bm.getWidth(), 0, 0,bm.getWidth(),bm.getHeight());
ByteBuffer buffer = ByteBuffer.allocate(bm.getRowBytes()*bm.getHeight());
bm.copyPixelsToBuffer(buffer);//I copy the pixels from Bitmap bm to the buffer
ByteBuffer buffer1 = ByteBuffer.wrap(buffer.array());
newbm = Bitmap.createBitmap(160, 160,Config.RGB_565);
newbm.copyPixelsFromBuffer(buffer1);//I read pixels from the Buffer and put the pixels to the Bitmap newbm.
imageview1.setImageBitmap(newbm);
imageview2.setImageBitmap(bm);
Run Code Online (Sandbox Code Playgroud)
为什么Bitmap bm和newbm没有显示相同的内容?
我试图从图像的路径获取位图图像.但是BitmapFactory.decodeStream回报null价值.
码:
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
connection.disconnect();
input.close();
Run Code Online (Sandbox Code Playgroud)
我在更多的网站上搜索过,但我还没有得到解决方案.
我目前遇到以下问题:我想转换来自具有以下配置的文件的字节数组:
Byte1: R color of pixel 0,0.
Byte2: G color of pixel 0,0.
Byte3: B color of pixel 0,0.
Byte4: R color of pixel 0,1.
...
ByteN: R color of pixel n,n.
Run Code Online (Sandbox Code Playgroud)
所以我想做的是将这些字节转换为位图,而不必逐像素设置,bitmap.setPixel因为它需要太长时间.
有什么建议?提前致谢!
我正在制作一个简单的绘图应用程序.
我希望能够在设备方向改变时将用户的绘图保存在屏幕上.这只发生在主要活动中.
我读到如果方向改变,那么活动将被销毁并再次重新创建(onCreate(Bundle created)被调用).我不确定它是否意味着它也应该调用onSavedInstanceState(Bundle bundle),因为在我的应用程序中,只有在另一个活动将焦点放在我的主要活动之上时才会调用它,而不是在旋转到横向/纵向时.
我只是想找到一种方法来保存现有的位图,并在方向改变时将其传递给主要活动.如果我onSaveInstanceState永远不会被召唤,我怎么能这样做?
此外,由于Bitmap parceable已实现,我使用它.
这是主要活动的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// some more activity code...
if (savedInstanceState != null) {
bitmap = savedInstanceState.getParcelable("bitmap");
Log.d("STATE-RESTORE", "bitmap created");
paintBoard.setBitmapBackground(bitmap, false);
Log.d("RESTORING...", "onRestoreInstanceState()");
} else {
Log.d("SavedInstanceState", "null");
}
}
// Never called when I change orientation on my device
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
bitmap = Bitmap.createBitmap(paintBoard.getBitmap());
outState.putParcelable("bitmap", bitmap);
Log.d("STATE-SAVE", "onSaveInstanceState()");
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
编辑:
我从AndroidManifest.xml文件中删除了这一行:
android:configChanges="orientation"
Run Code Online (Sandbox Code Playgroud)
现在 …